Puzzling output for password in lesson 1


#1

Running the example provided in lesson 1 outputs the line
Users: {"eric"=>668, "you"=>56789}
The codes is: my_machine = Machine.new("eric", 01234)
why 668 not 01234, (or 1234?).
Hint: Changing the code to
my_machine = Machine.new("eric", 1234)
Prints out
Users: {"eric"=>1234, "you"=>56789}
I can't work it out! Is the 0 in 01234 changing the number base?


#2

Hey switp

You were correct in that the 0 in front is changing the number base, as ruby reads it as base 8 instead.
There are other conversions that also work, which is 0x for Hexadecimal (base 16), and 0b for binary (base 2)

This link, a post of the old forum that mentions the same thing you are pointing out, says that the reason is rooted back to the early C specification days.

You can also read a bit more about octals here, if you're interested


#3

Thanks, interesting. I won't forget that. I can just see myself aligning a column of numbers with leading zeroes.