Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please explain how Ruby interprets numbers [duplicate]

Tags:

ruby

When the following numbers are entered into irb, I get the following results:

50 #=> 50
050 #=> 40
030 #=> 24
005 #=> 5

Why does 050 equal 40 rather than 50, 030 == 30, 005 == 5? What's happening?

like image 318
jchi2241 Avatar asked Dec 05 '25 20:12

jchi2241


2 Answers

Numbers starting with 0 are interpreted as octal numbers.

010 => 8
020 => 16
0777 => 511
like image 70
HirofumiTamori Avatar answered Dec 08 '25 10:12

HirofumiTamori


Octal numbers. Not just in Ruby - most programming languages think that when you start a number with a zero, it's in base 8.

  • 50 = 5 * 10
  • 050 = 5 * 8
like image 22
Amadan Avatar answered Dec 08 '25 12:12

Amadan