Ruby 如果其他 array.each
Ruby if else in array.each
这里是 Ruby 的新手。我试图在数组中放置一个 if else 语句,以便在某个模数 == 0 时发送一个字符串。对于我来说,我无法在任何地方找到它。我相信有人会觉得它简单得可笑。
a = *(1..100)
a.each { |i| puts "three" if i % 3 == 0 elsif puts "five" if i % 5 == 0 else puts i}
只是不确定语法是否正确。 ruby 还是个新手,正在努力学习语法。上学期考了 C class,我的大脑一直想输入 C 语法。
当我将其保留为
a = *(1..100)
a.each { |i| puts "three" if i % 3 == 0}
它工作正常,只是想弄清楚如何添加 if else。感谢帮助。
下面的回答真的很有帮助。我试图更进一步并将其调用到一个函数中。它一直返回 5,而不是 "five",或者 3,而不是 "three"。
这是我的函数:
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
有人告诉我它没有更新我的阵列。再次感谢。
你 可以 使用 if
来限定单个语句,但是一旦你进入 elsif/else
领域,就必须像普通 C 风格 if
语句:
a.each do |i|
if i % 3 == 0
puts "three"
elsif i % 5 == 0
puts "five"
else
puts i
end
end
语法如下。
a = *(1..100)
a.each do |i|
if i % 3 == 0
puts "three"
elsif i % 5 == 0
puts "five"
else
puts i
end
end
请记住,each
会 return Enumerator
,但不是精确值。您需要对 return 值使用 return
关键字。
这是 docs
请查看以下代码是否有帮助
def abc
a = *(1..100)
a.each do |i|
if i % 3 == 0
puts "three"
elsif i % 5 == 0
puts "five"
else
puts i
end
end
end
=> :abc
2.3.0 :013 > abc
这在 irb 模式下提供了所需的输出。
或者,如果你想强调,总是有一个 puts
,只是输出值发生变化:
puts(
if i%3==0
'three'
elsif i%5==0
'five'
else
i
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
其中 c_1
… c_n
和 e_1
… e_nplus1
可以是任意 Ruby 表达式。
可以使用表达式分隔符(即 ;
或换行符)代替 then
关键字来分隔条件表达式的各部分。
带分号(这种用法不符合惯用语):
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
如果您使用换行符,您也可以选择使用 then
关键字,但这也不是惯用语:
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
}
但是,对于这样的 if
/ elsif
链,使用 case
表达式通常更为惯用:
# 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
}
请注意,条件表达式(if
和 case
)是 表达式 ,而不是 语句 。 Ruby 中没有语句,一切都是表达式,一切都求值。条件表达式的计算结果为所采用的分支中表达式的值。
所以,你也可以这样写:
# 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)
}
这里是 Ruby 的新手。我试图在数组中放置一个 if else 语句,以便在某个模数 == 0 时发送一个字符串。对于我来说,我无法在任何地方找到它。我相信有人会觉得它简单得可笑。
a = *(1..100)
a.each { |i| puts "three" if i % 3 == 0 elsif puts "five" if i % 5 == 0 else puts i}
只是不确定语法是否正确。 ruby 还是个新手,正在努力学习语法。上学期考了 C class,我的大脑一直想输入 C 语法。
当我将其保留为
a = *(1..100)
a.each { |i| puts "three" if i % 3 == 0}
它工作正常,只是想弄清楚如何添加 if else。感谢帮助。
下面的回答真的很有帮助。我试图更进一步并将其调用到一个函数中。它一直返回 5,而不是 "five",或者 3,而不是 "three"。
这是我的函数:
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
有人告诉我它没有更新我的阵列。再次感谢。
你 可以 使用 if
来限定单个语句,但是一旦你进入 elsif/else
领域,就必须像普通 C 风格 if
语句:
a.each do |i|
if i % 3 == 0
puts "three"
elsif i % 5 == 0
puts "five"
else
puts i
end
end
语法如下。
a = *(1..100)
a.each do |i|
if i % 3 == 0
puts "three"
elsif i % 5 == 0
puts "five"
else
puts i
end
end
请记住,each
会 return Enumerator
,但不是精确值。您需要对 return 值使用 return
关键字。
这是 docs
请查看以下代码是否有帮助
def abc
a = *(1..100)
a.each do |i|
if i % 3 == 0
puts "three"
elsif i % 5 == 0
puts "five"
else
puts i
end
end
end
=> :abc
2.3.0 :013 > abc
这在 irb 模式下提供了所需的输出。
或者,如果你想强调,总是有一个 puts
,只是输出值发生变化:
puts(
if i%3==0
'three'
elsif i%5==0
'five'
else
i
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
其中 c_1
… c_n
和 e_1
… e_nplus1
可以是任意 Ruby 表达式。
可以使用表达式分隔符(即 ;
或换行符)代替 then
关键字来分隔条件表达式的各部分。
带分号(这种用法不符合惯用语):
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
如果您使用换行符,您也可以选择使用 then
关键字,但这也不是惯用语:
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
}
但是,对于这样的 if
/ elsif
链,使用 case
表达式通常更为惯用:
# 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
}
请注意,条件表达式(if
和 case
)是 表达式 ,而不是 语句 。 Ruby 中没有语句,一切都是表达式,一切都求值。条件表达式的计算结果为所采用的分支中表达式的值。
所以,你也可以这样写:
# 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)
}