irb> 1
=> 1
irb> 1.to_s
=> "1"
irb> 1.to_s * 2
=> "11"
irb> "1" *2
=> "11"
irb> 1.to_s *2
=> "1" # huh?
#let's try that again with 2....
irb> 2.to_s
=> "2"
irb> 2.to_s * 2
=> "22" # good
irb> "2" *2
=> "22"
irb> 2.to_s *2
=> "10" # wtf?
Sometimes a space makes all the difference. Ruby's #to_son an Integer takes an optional parameter which specifies the base you are working in. In this case, passing in *2 has set our base to binary.
Comments
Post a Comment