This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

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?

Task Discussion


  • Roelof said:

    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

    on Aug. 29, 2013, 12:30 p.m.

    Andre Dublin said:

    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.

    on Aug. 29, 2013, 1:10 p.m. in reply to Roelof

    Roelof said:

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

     

    on Aug. 29, 2013, 1:16 p.m. in reply to Andre Dublin

    Andre Dublin said:

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

    on Aug. 29, 2013, 1:17 p.m. in reply to Roelof

    Roelof said:

    Thanks Can you recommend a good tutorial about that.

     

    on Aug. 30, 2013, 10:13 a.m. in reply to Andre Dublin

    Andre Dublin said:

    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/

    on Aug. 30, 2013, 10:24 a.m. in reply to Roelof
  • Si Dunn said:

    I've posted mine here...

     

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

    on Nov. 28, 2012, 4:31 p.m.
  • Jure said:

    on Oct. 29, 2012, 10:14 a.m.
  • David O'Dowd said:

    on Oct. 11, 2012, 11:51 p.m.

    Andre Dublin said:

    Looking good David, sorry for the late response.

    on Oct. 19, 2012, 4:09 p.m. in reply to David O'Dowd
  • Ajiwo said:

    on Aug. 30, 2012, 9:16 a.m.
  • sedmonds said:

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

    on March 29, 2012, 2:03 p.m.
  • Peter Hwang said:

    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. 

    on March 22, 2012, 12:59 a.m.

    Anonym said:

    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.

    on March 22, 2012, 2:16 a.m. in reply to Peter Hwang

    Andre Dublin said:

    Yes, questions will take a little research.

    on March 22, 2012, 9:01 a.m. in reply to Anonym
  • Haitham Gasim said:

    Hi ... 

    Where should we put the answers to the discussion questions?

    Thanks.

     

    Haitham

    on March 19, 2012, 11:32 p.m.

    Haitham Gasim said:

    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 
    on March 20, 2012, 8:17 a.m. in reply to Andre Dublin