This is the article on the 16th day of Ruby Part 2 Advent Calendar 2020. Yesterday was shxun6934's Ruby from the perspective of another language. Part 2 is empty, so I will post an article because it is a big deal.
It's not a useful story, but a Ruby story.
However, 0o
and 0
start with octal numbers, which is a specification that is common not only in Ruby but also in major programming languages, so if you do not know it, you may want to know it.
In recent languages, the impression is that only the beginning of 0o
is allowed and the beginning of 0
is a syntax error.
Numeric literals starting with 0b
are interpreted as binary numbers to create numbers.
Numeric literals starting with 0o
and 0
are interpreted as octal numbers to create numbers.
Numeric literals starting with 0x
are interpreted as hexadecimal numbers to create numbers.
p 10 #=> 10
p 0b10 #=> 2
p 0o10 #=> 8
p 010 #=> 8
p 0x10 #=> 16
To summarize in the table, it looks like the following.
prefix | n-ary | English | Other |
---|---|---|---|
0b |
Binary number | binary | |
0o |
8 base | octal | mere0 始まりは8 base |
0d |
Decimal number | decimal | 1 〜9 始まりはDecimal number |
0x |
Hexadecimal | hexadecimal |
Note that prefix works in the same way with uppercase letters.
Since an octal number is a number consisting of eight numbers from 0 to 7, if there are other 8s and 9s, it cannot be interpreted as an octal number and a syntax error occurs.
(* It is difficult to understand because it is not displayed as syntax error
, but it is not executed even if other code is written before, and it is certainly a syntax error.)
p 0190
# Main.rb:1: Invalid octal digit
# p 0190
# ^~~
It politely tells you Invalid octal digit
.
However, if you do this in binary in the same way, you will not understand the error statement.
If the binary number has a number other than 0 and 1, it's the same as the octal number.
I want it to be displayed as Invalid binary digit
.
p 0b12
# Main.rb:1: syntax error, unexpected integer literal, expecting end-of-input
# p 0b12
# ^
When I try it, it is the same up to the point where the syntax error occurs.
However, I get angry with unexpected integer literal
.
If you look closely, the position that points to the numerical value of the error statement is also different from that of the octal number.
The location of the error pointed to by the octal number is 0
in the first prefix.
On the other hand, the position of the error pointed to by the binary number is 2
.
Apparently, unlike octagonal numbers, binary numbers up to 0b1
are properly interpreted as binary numbers, and then
It seems to be an error that another integer value comes right next to that number and the syntax does not hold.
So what if you bring the number 2
right next to b
?
Neither 0b
nor 0b2
can be a binary number. It seems to be different from the previous error statement.
p 0b2
# Main.rb:1: numeric literal without digits
# p 0b2
# ^~
# Main.rb:1: syntax error, unexpected integer literal, expecting end-of-input
# p 0b2
# ^
Interpreting 0b
and 2
separately, giving an error with the former 0b
as numeric literal without digits
(numerical literal without numbers), and the former and the latter being unnaturally continuous. It seems that "( 2
) is an unexpected numerical literal "is issued as an error statement.
Eighth numbers were simple errors, but binary literal error statements are confusing.
When I run Ruby 1.8, I already get the error statement "Illegal octal digit" at 090
.
Then, when it reached 1.9, it changed to the current word "Invalid octal digit".
It seems that this area has always been a kind error statement.
What is the difference between an octal number and other numeric literal error statements?
If you interpret and guess in your own way, it is as follows.
Numerical numbers can be combined starting with 0
without alphabets.
Then, there are cases where numbers are used for phone numbers and times and written in decimal literals starting with 0
as if they were decimal numbers, so it is easy to notice errors in cases where they are unintentionally used as decimal numbers. I think I tried to kindly teach the error only in decimal numbers.
However, although errors such as binary numbers may be rare cases, the content of the error sentence is still difficult to understand and it is a level that can not be understood unless you do your best to unravel it. It doesn't hurt me directly, but it bothers me.
Eighth error statements are kind and easy to solve if you google, but conversely, binary numbers are not at a level where you can easily understand what you want to say. Well, it's made by humans, and I feel that these details are out of reach. Ideally, I wanted to do something about these small levels, so I wrote an article.