标签: ruby arrays
我想知道是否有办法在一行中初始化这个数组:
mulof = [] (1..1000).each {|i| mulof << i if (i % 3 == 0 || i % 5 == 0)}
答案 0 :(得分:4)
是的,使用Enumerable#select:
Enumerable#select
mulof = (1..1000).select { |i| i % 3 == 0 || i % 5 == 0 }