What does the underscore mean in literal numbers?

Ruby

Ruby Problem Overview


What does that mean?
0.0..10_000.0

Ruby Solutions


Solution 1 - Ruby

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

Solution 2 - Ruby

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

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

Solution 3 - Ruby

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).

Solution 4 - Ruby

Actually all other answers here are wrong.

_ is not ignored, just try it with 0_50:

> 1_50
 => 150
> 0_50
 => 40

YEAAAAAAH YOU WILL FREAK OUT IF YOU JUST WANTED TO USE IT FOR DECIMALS :(

In general it just describes a range of numbers, like CCD mentions above.

As Kyle Heironimus commented, the underscore is actually ignored.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionauralbeeView Question on Stackoverflow
Solution 1 - RubyKyle HeironimusView Answer on Stackoverflow
Solution 2 - RubyzoulView Answer on Stackoverflow
Solution 3 - RubyCCDView Answer on Stackoverflow
Solution 4 - RubyBvuRVKyUVlViVIc7View Answer on Stackoverflow