Hi,
I actually registered here to ask a different question, but after
checking the core docs for 1.8.7 I realized that the behavior has
apparently changed, but as part of the registration process I was asked
to enter the result of:
puts ((11.to_s * 2).to_i/2)
I don’t know Ruby very well, but this one seemed pretty
straight-forward, in that it was supposed to convert the number (object)
11 to a string, double it (‘1111’) then convert it back to an integer
and divide by two. Since these are integers the answer would be: 555.
But here’s where it gets strange. Since I’m new to Ruby I thought I’d
play with it a bit, and since I thought that whitespace wasn’t supposed
to matter, I did:
11.to_s * 2
=> “1111”11.to_s *2
=> “1011”11.to_s*2
=> “1111”
I thought, maybe this is a problem with irb, so I wrote a little
program:
#!/usr/bin/env ruby
puts (11.to_s * 2)
puts (11.to_s 2)
puts (11.to_s2)
And when I run it, I see:
1111
1011
1111
Can anyone explain why the ‘11.to_s *2’ gave me 1011? Is this a 1.8.7
problem? The Ruby version on my Mac OS X Lion system is: ruby 1.8.7
(2010-01-10 patchlevel 249) [universal-darwin11.0].
Thanks,
Rob