LoginSignup
21
15

More than 5 years have passed since last update.

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

Posted at
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]
21
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
15