Posted at

rubyのnextに引数与えるとnil返さずにすむのしらなんだ

More than 5 years have passed since last update.


iterator_next.rb

a = [1,2,3,4,5,6,7,8,9,10]

a = a.map do |i|
#next if i.even?
next i if i.even?
i*10
end
p a
#=>[10, nil, 30, nil, 50, nil, 70, nil, 90, nil]
#=>[10, 2, 30, 4, 50, 6, 70, 8, 90, 10]