The Zen of Ruby 10/20


#1

Maybe someone could help me out!

I do not understand the error displayed when I type the following code:

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

my_array.each{ |x| if x even? puts x}

Error displayed:
(ruby):2: syntax error, unexpected '}', expecting keyword_then or ';' or '\n'

Thanks a million!


#2

You can write it this way

Option 1
my_array.each { |x| puts x if x.even? }

option 2
my_array.each { |x| if x.even? ; puts x; end}

option 3
my_array.each do |x|
if x.even?
puts x
end
end