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.
position = 14 position = position - position%3 => 12
上面的代码有效,但我不愿意引用position三遍。++我在上面尝试做的事情是否有等价物?我设想类似position =-%3.
position
++
position =-%3
我在 ruby-docs 中找不到任何内容。任何人?
或者(仅适用于某些品种):
position = (position / 3) * 3
这假设position持有一个整数,而不是一个浮点数(如14.0)。在这种情况下,您可以这样做(position.to_i / 3) * 3。
14.0
(position.to_i / 3) * 3