Reflecting on Exercise 1


Making it stick

Time to put your thinking hats on!

1) What is the difference between puts and print?

2) For big numbers (thousands, millions, billions etc) what is an alternative syntax that ruby provides? Provide an example.

3) What do these methods do? to_s, to_i, puts, gets, chomp

4) What is the programming term when you place a variable inside a string like so: "Author is #{author_age_in_years} years old."

Extra Credit: Explain why in Ruby you can use the "+" operator to add numbers and build strings?

Diskutera uppgift


  • Roelof sa:

    Here my answers :

     

    https://github.com/roelof1967/homework/tree/master/reflecting%20exercise%201

     

    Again I hope someone will look if the answers are allright

     

    Roelof

    i 29 augusti 2013 12:30

    Andre Dublin sa:

    For the second question

    If you open up irb (ruby interpreter) in your console and input what you provided you will be given an error

    irb(main):001:0> 1.000.000.000
    SyntaxError: (irb):1: no . floating literal anymore; put 0 before dot
    1.000.000.000
          ^
    (irb):1: syntax error, unexpected tINTEGER
    1.000.000.000
             ^
    (irb):1: no . floating literal anymore; put 0 before dot
    1.000.000.000
              ^
    	from /usr/local/var/rbenv/versions/1.9.3-p392/bin/irb:12:in `'
    

    The correct syntax is 

    irb(main):002:0> 1_000_000_000
    => 1000000000
    

    For the extra credit question:

    The reason that you can apply the "+" operator to number and strings is because of Ruby's object inheritance.  You will come to find out that Ruby objects all inherit from Object and there are methods hidden up the chain that allow them to be applied to different types of objects.  If you open your ruby interpreter and type in the following, it will give you a basic understanding of how Ruby finds its methods

    1.class.superclass.class.superclass.superclass
    

    This is the road down metaprogramming in ruby.

    i 29 augusti 2013 13:10 som svar på Roelof

    Roelof sa:

    Thanks for the feedback. Can I continue or can I better read more about this subject.

     

    i 29 augusti 2013 13:16 som svar på Andre Dublin

    Andre Dublin sa:

    Yes you can contiune. And I highly recommend reading more about metaprogramming in ruby.

    i 29 augusti 2013 13:17 som svar på Roelof

    Roelof sa:

    Thanks Can you recommend a good tutorial about that.

     

    i 30 augusti 2013 10:13 som svar på Andre Dublin

    Andre Dublin sa:

    Nothing can beat this book when it comes to learning about this topic

    http://www.amazon.com/Metaprogramming-Ruby-Program-Like-Pros/dp/1934356476

    As for tutorials online, Im sure you can do a search online and find a lot of sources out there.

    But one that I recommend are from the rubylearning.com guys

    http://ruby-metaprogramming.rubylearning.com/

    i 30 augusti 2013 10:24 som svar på Roelof
  • Si Dunn sa:

    I've posted mine here...

     

    https://github.com/sidunn/coursework/blob/master/thinking_caps1.rb

    i 28 november 2012 16:31
  • Jure sa:

    i 29 oktober 2012 10:14
  • David O'Dowd sa:

    i 11 oktober 2012 23:51

    Andre Dublin sa:

    Looking good David, sorry for the late response.

    i 19 oktober 2012 16:09 som svar på David O'Dowd
  • Ajiwo sa:

    i 30 augusti 2012 09:16
  • sedmonds sa:

    Submitted: https://github.com/sedmonds/p2pu_ltp/blob/master/Week-1/reflections.rb

    i 29 mars 2012 14:03
  • Peter Hwang sa:

    I don't remember reading about prints.  Can someone point me where we were supposed to learn that?  Same goes for placing a variable inside a string.  Not sure how I missed those two concepts. 

    i 22 mars 2012 00:59

    Anonym sa:

    Hi Peter,

    You are right about the string. LTP doesnt mention this in first chapters. I have picked up up the placing variable inside a string method from other resources (ruby-doc, first chapter about printing strings). I also use 'Beginning Ruby' from Apress.

    i 22 mars 2012 02:16 som svar på Peter Hwang

    Andre Dublin sa:

    Yes, questions will take a little research.

    i 22 mars 2012 09:01 som svar på Anonym
  • Haitham Gasim sa:

    Hi ... 

    Where should we put the answers to the discussion questions?

    Thanks.

     

    Haitham

    i 19 mars 2012 23:32

    Haitham Gasim sa:

    Great.

    Here you go.

     

    1) What is the difference between puts and print?
     
       puts adds a newline character if it's not present at the end of the string while print doesn't.
     
    2) For big numbers (thousands, millions, billions etc) what is an alternative syntax that ruby provides? Provide an example.
     
       underscores can replace comma separators without any rules. Eg: 25000000 can be written as 25_000_000 which is the equivalent of 25,000,000
     
    3) What do these methods do? to_s, to_i, puts, gets, chomp
     
       to_s: produces the string verion of an object
       to_i: produces the integer version of an object
       puts: (put string) writes out an object by applying to_s to it to get its string version
       gets: reads a string from the input until return is hit
       chomp: removes trailing edge new line characters from input strings
     
    4) What is the programming term when you place a variable inside a string like so: "Author is #{author_age_in_years} years old."
     
       Embedded Evaluation: will evaluate only strings enclosed with double quotes
     
    Extra Credit: Explain why in Ruby you can use the "+" operator to add numbers and build strings?
     
    "+" in Ruby will output the concatenation of muliple strings and the sum of multiple numbers:
     
    puts 'dog ' + 'cat' will output dog cat
    puts 1 + 1 will output 
    i 20 mars 2012 08:17 som svar på Andre Dublin