LoginSignup
2
2

More than 5 years have passed since last update.

Rationalリテラル(Ruby2.1から)

Last updated at Posted at 2015-01-17

x = 1/2r
p x * 2
#=> (1/1) 

rサフィックスでRationalインスタンスを作れるか、うろ覚えだったので例外が発生するのかなと思いましたがそんなことはありませんでした。

pryで実験


pry(main)> Rational(1, 2)
=> (1/2)

pry(main)> 1/2r
=> (1/2)

pry(main)> Rational(1, 2) * 2
=> (1/1)

pry(main)> 1/2r * 2
=> (1/1)

参考リンク

Ruby 2.1.0 リリース (2.0 からの変更点)

2.0 からの変更点
.....
文法の変更
Rational/Complex リテラル #8430
defの戻り値 #3753
...

参考にしたツイート

以下のツイートにインスパイアされて記事を書きました。
参考: 武田哲也さんのRuby技術者認定試験受験記

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2