each with block which has if statement : each « Array « Ruby






each with block which has if statement


[1,2,3].each {|i| puts i}
# 1
# 2
# 3
[1,2,3].each do |i|
  if i % 2 == 0
    puts "#{i} is even."
  else
    puts "#{i} is odd."
  end
end
# 1 is odd.
# 2 is even.
# 3 is odd.

 








Related examples in the same category

1.Arrays and Blocks
2.Assign block value to a variable in array each method
3.Iterating Over an Array with each
4.LocalJumpError: no block given