[Ruby] Express n-ary numbers as numbers starting from 0

Numbers starting from 0

Example → 09

irb(main):027:0> 09

If you hit the enter key here, it becomes ↓.

irb(main):027:0> 09
SyntaxError: (irb):27: Invalid octal digit
	from /Users/username/.rbenv/versions/2.1.5/bin/irb:11:in `<main>'

Example → 0x10

irb(main):028:0> 0x10

If you hit the enter key here, it becomes ↓.

irb(main):028:0> 0x10
=> 16

Cardinal indicator

List

Base number Cardinal indicator
Binary number 0b
8 base 0o or 0
Decimal number 0d
Hexadecimal 0x

Example

As shown in the ** list ** above, the number after the radix specifier is converted to the corresponding radix. If the above notation does not apply, an error will occur as in ** Example → 09 **.

irb(main):047:0> 0b10
=> 2
irb(main):049:0> 0o10
=> 8
irb(main):050:0> 010
=> 8
irb(main):051:0> 0d10
=> 10
irb(main):052:0> 0x10
=> 16

References

[[Revised 2nd Edition] Ruby Engineer Certification Exam Passed Textbook (Silver / Gold Compatible) Ruby Official Qualification Textbook](https://www.amazon.co.jp/%E6%94%B9%E8%A8%822% E7% 89% 88-Ruby% E6% 8A% 80% E8% A1% 93% E8% 80% 85% E8% AA% 8D% E5% AE% 9A% E8% A9% A6% E9% A8% 93% E5% 90% 88% E6% A0% BC% E6% 95% 99% E6% 9C% AC-Silver-Gold% E5% AF% BE% E5% BF% 9C-Ruby% E5% 85% AC% E5% BC% 8F% E8% B3% 87% E6% A0% BC% E6% 95% 99% E7% A7% 91% E6% 9B% B8 / dp / 4774191949) (Not an affiliate link)

Recommended Posts

[Ruby] Express n-ary numbers as numbers starting from 0
IntelliJ starting from 1
Safe numbers (ruby edition)
Picture-in-picture starting from iOS14
Ruby Learning # 9 Math & Numbers