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.
我目前正在学习 Ruby,我看到了这个声明:
the_count = [1, 2, 3, 4, 5]
但是,我觉得有一种更聪明的方法可以用预先存在的函数声明这个数组。我想知道这个特定数组是否有一个术语,其内容随着其索引上升 1 而上升 1。提前致谢!
使用范围。
the_count = [*1..5]
...或者...
the_count = (1..5).to_a
the_count = *(1..5)
(感谢Meagar。)