Why is 032 different than 32 in Ruby?

前端 未结 4 632
一生所求
一生所求 2021-01-07 10:17

I have noticed Ruby behaves differently when working with 032 and 32. I once got syntax errors for having 032 instead of just 32 in my code. Can someone explain this to me?

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-07 10:52

    When you have a zero in front of your number, Ruby interprets it as an octal(base 8 number).

    You syntax error is probably something like this:

    ruby-1.9.2-p136 :020 > 08
    SyntaxError: (irb):20: Invalid octal digit
    

提交回复
热议问题