# 数组 – 在array.each中的Ruby if else

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

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

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

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

Ruby中条件表达式的语法是：

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

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 c_1
e_1
elsif c_2
e_2
elsif c_3
e_3
# …
elsif c_n
e_n
else
e_nplus1
end

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

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

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

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

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

# idiomatic
case when c_1 then e_1 when c_2 then e_2 when c_3 then e_3 … when c_n then 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
then e_1
when c_2
then e_2
when c_3
then e_3
# …
when c_n
then e_n
else
e_nplus1
end

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

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

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

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

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

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

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

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

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

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

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

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