jeudi 1 septembre 2016

Ruby if else in array.each

New to Ruby here. I am attempting to put an if else statement in an array to send a string if a certain modulus == 0. for the life of me i can't find it anywhere. I am sure someone will find it ridiculously simple.

a = *(1..100)
a.each { |i| puts "three" if i % 3 == 0 elsif puts "five" if i % 5 == 0 else puts i}

Just not sure of the correct syntax. Still new to ruby and am trying to learn the syntax. Took a C class last semester and my brain keeps wanting to put in C syntax.

When I leave it as

a = *(1..100)
a.each { |i| puts "three" if i % 3 == 0}

it works fine, just trying to figure out how to add if else to it. Help is appreciated.

Aucun commentaire:

Enregistrer un commentaire