Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有人可以向我解释一下吗?
x = Rational(3/4) * 8 => (0/1) # I Expected it to return 6 x.to_i => 0
谢谢。
您正在创建一个作为唯一参数的Rational数字。是,所以,你的代码相当于3/43/40
Rational
3/4
0
Rational(0) * 8
这显然是0。
将此与
Rational(3, 4) * 8 # => (6/1)
您在其中显式传递分子和分母。
如果您更喜欢在分数中使用斜线,您可以使用字符串作为参数:
x = Rational('3/4') * 8
或者
x = ('3/4'.to_r) * 8