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.
为什么这个表达式计算为 13 ?
我不小心评估了这个表达式(1_2).next而不是(1+2).nextwhich o/p 4 结果。
(1_2).next
(1+2).next
=> (1_2).next => 13
请让我知道这是怎么回事,因为我是 Ruby 新手
Ruby 允许你用来_分解长数字,例如
_
123456789 == 123_456_789
但后者更容易阅读,所以你的代码是一样的12.next
12.next
1_2 与 12 相同。12.next 为 13。数字中的下划线被忽略,您可以使用它们来提高可读性。例如,1000_000_000 是十亿。