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.
所以这会上升
(1..5).to_a => [1, 2, 3, 4, 5]
但这并不
(5..1).to_a => []
我试图从任意天花板上得到一个降序。谢谢。
试试这个:
5.downto(1).to_a # => [5, 4, 3, 2, 1]
当然,也有对应的#upto。如果你想要步骤,你可以这样做:
#upto
1.step(10, 2).to_a # => [1, 3, 5, 7, 9] 10.step(1, -2).to_a # => [10, 8, 6, 4, 2]
或者你可以试试这个: (1..5).to_a.reverse # => [5, 4, 3, 2, 1]
(1..5).to_a.reverse # => [5, 4, 3, 2, 1]