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
This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.
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?
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
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.
Thanks for the feedback. Can I continue or can I better read more about this subject.
Yes you can contiune. And I highly recommend reading more about metaprogramming in ruby.
Thanks Can you recommend a good tutorial about that.
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've posted mine here...
https://github.com/sidunn/coursework/blob/master/thinking_caps1.rb
I've done it here:
https://raw.github.com/Hoornet/coursework/master/thinking_hat_1.rb
Looking good David, sorry for the late response.
Submitted: https://github.com/sedmonds/p2pu_ltp/blob/master/Week-1/reflections.rb
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.
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.
Yes, questions will take a little research.
Hi ...
Where should we put the answers to the discussion questions?
Thanks.
Haitham
Great.
Here you go.