ruby - Print only even numbers from an array -


i'm working way through ruby tutorial. 1 of questions provides array. have to:

"write loop puts values of my_array. (bonus points if use one-line if!)"

my answer passes test shows syntax error, answer if statement below. can explain why syntax error?

my_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  if my_array.each |x| % 2 == 0 puts x 

you're missing end statement do , left operand modulus. try this:

my_array = (1..10).to_a my_array.each { |x| puts x if x.even? } 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -