What does the underscore mean in literal numbers?

It is a Range object, of the kind a..b

In this case it gives you the numbers from 0 to 10,000 as Floats.

the underscore '_' is ignored, and used for readability, so 10_000 is equivalent 10,000.

Buy adding .0 to each part of the range, the numbers would be considered as floats instead of integers, so you won't be able to iterate over the range (the each method would raise an exception).


It’s just a syntax convenience to separate the thousands:

$ ruby -e 'puts 1_000 + 1_000_000'  #=> 1001000

Underscores are ignored. You can put them in to make them more readable.

Tags:

Ruby