What is this syntax for determining binary numbers in a ruby?

some terminal output is worth a thousand words, so let's start with this:

[10] pry(main)> 1_000
=> 1000

yep, we can define thousands in readable form in ruby, I know that everything is fine. Hey, I wonder what will happen if I try to leave the panel with zeros?

[9] pry(main)> 001_000
=> 512

Well, that’s weird, it’s not binary, as it would be 8 hmm ...

[20] pry(main)> 01_0
=> 8

so 8 ... ok, 2 ** 3 is 8, 2 ** (3 * 3) - 512. The bet 01_00 is 2 ** 6 == 64

[24] pry(main)> 01_00
=> 64

hmm ... there is nothing special about emphasizing the syntax of numbers, just to make it look beautiful:

[23] pry(main)> 0100
=> 64

So, what are these numbers called (they are not direct binary ... I try to think about what they should do, but continue to come up with a space). Also, why are they so important? can anyone post a link to the documentation describing them?

+4
1

0, .

, 0x 0x, .

0x10
# => 16
0x100
# => 256

BTW, , , % operator sprintf:

'%o' % 512
=> "1000"

'%x' % 256
# => "100"

'%x' % 512
# => "200"

'%b' % 3
# => "11"
+9

Source: https://habr.com/ru/post/1539458/


All Articles