Differences between Ruby syntax error statements in Ruby and binary

At first

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.

Talking about octal and binary error statements

As a premise, binary and octal literals

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 19始まりはDecimal number
0x Hexadecimal hexadecimal

Note that prefix works in the same way with uppercase letters.

8-base syntax error

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.

Binary syntax error

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.

Why octal errors are kind

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.

Recommended Posts

Differences between Ruby syntax error statements in Ruby and binary
[Understanding] Differences between hashes and arrays in Ruby
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Differences between Ruby strings and symbols [Beginner]
Ruby: Differences between class methods and instance methods, class variables and instance variables
[Ruby] Difference between receiver and object. Differences between Ruby objects and JS objects
About the difference between classes and instances in Ruby
In fact, Ruby distinguishes between line breaks and whitespace.
Differences between Fetch Type LAZY and EAGER in Hibernate
Ruby syntax errors and countermeasures
Differences between IndexOutOfBoundsException and ArrayIndexOutOfBoundsException
Differences between namespace, module, scope and as in Rails routing
About the difference between "(double quotation)" and "single quotation" in Ruby
Think about the differences between functions and methods (in Java)
Differences in how to handle strings between Java and Perl
[Ruby] Difference between get and post
Differences between "beginner" Java and Kotlin
[Ruby] then keyword and case in
After switching between Java 8 and 11 versions, an error occurs in confirmation
Write keys and values in Ruby
Differences between Applet class and JApplet class
Differences between Java and .NET Framework
[Ruby] Summary of exception classes that appear in error statements [Meaning of NameError]
Note: Difference between Ruby "p" and "puts"
Difference between final and Immutable in Java
Make bubble sort and selection sort in Ruby
Differences in writing in Ruby, PHP, Java, JS
Difference between Ruby instance variable and local variable
Difference between pop () and peek () in stack
Differences between preface and postfix of operators
[Java] Differences between instance variables and class variables
Summary of hashes and symbols in Ruby
Difference between getText () and getAttribute () in Selenium
[Ruby] Classification and usage of loops in Ruby
Tips for gRPC error handling in Ruby
[Ruby] Difference between print, puts and p
Difference between int and Integer in Java
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
Organize your own differences in writing comfort between Java lambda expressions and Kotlin lambda expressions.
Differences between Spring Initializr packaging JAR and WAR
Difference between next () and nextLine () in Java Scanner
Implement the algorithm in Ruby: Day 3 -Binary search-
Put CSV files containing "'" and "" "in MySQL in Ruby 2.3
Differences in writing Java, C # and Javascript classes
Summarize the differences between C # and Java writing
[Ruby] Assign variables in conditional expressions in if statements.
[Ruby] Difference between puts and return, output and return value
Distinguish between positive and negative numbers in Java
Ruby How to convert between uppercase and lowercase
Just put `clang 12.0.0` in macOS` Catalina` and put `ruby 3.0.0`