Ruby if else in array.each -


new ruby here. attempting put if else statement in array send string if modulus == 0. life of me can't find anywhere. sure find ridiculously simple.

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

just not sure of correct syntax. still new ruby , trying learn syntax. took c class last semester , brain keeps wanting put in c syntax.

when leave as

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

it works fine, trying figure out how add if else it. appreciated.

the answers below helpful. trying take step further , call function. keeps returning 5, , not "five", or 3, , not "three".

here function:

def array_mod = *(1..100) a.each { |i| if % 3 == 0  && % 5 == 0; = "fifteen" elsif % 3 == 0; = "three" elsif % 5 == 0; = "five" else = end } 

end

and here attempt @ calling it.

require "minitest/autorun" require_relative "array_modulus.rb"    class testarrayfunction < minitest::test   def test_array1      results = array_mod      assert_equal(100, results.length) end  def test_array2      results = array_mod     assert_equal("three", results[2]) end   end 

i told not updating array. again.

the syntax of conditional expression in ruby is:

if c_1 e_1 elsif c_2 e_2 elsif c_3 e_3 … elsif c_n e_n else e_nplus1 end 

where c_1c_n , e_1e_nplus1 can arbitrary ruby expressions.

it possible use expression separator (i.e. ; or newline) instead of then keyword separate parts of conditional expression.

with semicolon (this usage non-idiomatic):

if c_1; e_1 elsif c_2; e_2 elsif c_3; e_3 … elsif c_n; e_n else e_nplus1 end 

with newlines:

if c_1   e_1 elsif c_2   e_2 elsif c_3   e_3 # … elsif c_n   e_n else   e_nplus1 end 

if use newlines, can optionally use then keyword, non-idiomatic, too:

if c_1 e_1 elsif c_2 e_2 elsif c_3 e_3 # … elsif c_n e_n else   e_nplus1 end 

so, in case, correct syntax be:

# idiomatic a.each { |i| if % 3 == 0 puts "three" elsif % 5 == 0 puts "five" else puts end }  # non-idiomatic a.each { |i| if % 3 == 0; puts "three" elsif % 5 == 0; puts "five" else puts end }  # idiomatic a.each { |i|   if % 3 == 0     puts "three"   elsif % 5 == 0     puts "five"   else     puts   end }  # non-idiomatic a.each { |i|   if % 3 == 0   puts "three"   elsif % 5 == 0   puts "five"   else     puts   end } 

however, such chain of if / elsif, typically more idiomatic use case expression:

# idiomatic case when c_1 e_1 when c_2 e_2 when c_3 e_3 … when c_n e_n else e_nplus1 end  # non-idiomatic case when c_1; e_1 when c_2; e_2 when c_3; e_3 … when c_n; e_n else e_nplus1 end  # idiomatic case when c_1   e_1 when c_2   e_2 when c_3   e_3 # … when c_n   e_n else   e_nplus1 end  # non-idiomatic case when c_1 e_1 when c_2 e_2 when c_3 e_3 # … when c_n e_n else   e_nplus1 end 

which in case this:

# idiomatic a.each { |i| case when % 3 == 0 puts "three" when % 5 == 0 puts "five" else puts end }  # non-idiomatic a.each { |i| case when % 3 == 0; puts "three" when % 5 == 0; puts "five" else puts end }  # idiomatic a.each { |i|   case   when % 3 == 0     puts "three"   when % 5 == 0     puts "five"   else     puts   end }  # non-idiomatic a.each { |i|   case   when % 3 == 0   puts "three"   when % 5 == 0   puts "five"   else     puts   end } 

note conditional expressions (both if , case) expressions, not statements. there no statements in ruby, expression, evaluates value. conditional expression evaluates value of expression in branch taken.

so, write this:

# idiomatic a.each { |i| puts(if % 3 == 0 "three" elsif % 5 == 0 "five" else end) }  # non-idiomatic a.each { |i| puts(if % 3 == 0; "three" elsif % 5 == 0; "five" else end) }  # idiomatic a.each { |i|   puts(if % 3 == 0     "three"   elsif % 5 == 0     "five"   else       end) }  # non-idiomatic a.each { |i|   puts(if % 3 == 0   "three"   elsif % 5 == 0   "five"   else       end) }  # idiomatic a.each { |i| puts(case when % 3 == 0 "three" when % 5 == 0 "five" else end) }  # non-idiomatic a.each { |i| puts(case when % 3 == 0; "three" when % 5 == 0; "five" else end) }  # idiomatic a.each { |i|   puts(case   when % 3 == 0     "three"   when % 5 == 0     "five"   else       end) }  # non-idiomatic a.each { |i|   puts(case   when % 3 == 0   "three"   when % 5 == 0   "five"   else       end) } 

Comments