the industrial

ブログと言うより自分のためのメモ以外の何モノでもないです。でも読んでくださってありがとうございます。

Rubyで0始まりの数字は、デフォルトで8進数(基数8)になるよというお話

RSpecでテストケースを書いていたときの話。

expect(hoge_date).to eq Date.new(2018,01,01)
・・・
expect(hoge_date).to eq Date.new(2018,09,01)

としたとき、

Date.new(2018,09,01)
SyntaxError: (eval):2: Invalid octal digit
Date.new(2018,09,01)

のような事を言われてしまう。

09 の部分が問題で、Rubyでは0始まりの数字はデフォルトで8進数になるから。

知らなかったのだが、Shellでも同じなため割と一般的なのか?

ちなみに

expect(hoge_date).to eq Date.new(2018,01,01)
・・・
expect(hoge_date).to eq Date.new(2018,09,01)

expect(hoge_date).to eq Date.new(2018,01,01)
・・・
expect(hoge_date).to eq Date.new(2018, 9,01)

こう書きました😇