Arrays 数组中的Ruby if else.each
这里是Ruby的新手。我试图将if-else语句放入数组中,以便在某个模数==0时发送字符串。就我的一生而言,我在任何地方都找不到它。我相信有人会觉得它简单得可笑Arrays 数组中的Ruby if else.each,arrays,ruby,if-statement,each,Arrays,Ruby,If Statement,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课,我的大脑一直想输入C语法 当我把它当作 a = *(1.
a = *(1..100)
a.each { |i| puts "three" if i % 3 == 0 elsif puts "five" if i % 5 == 0 else puts i}
只是不确定语法是否正确。对ruby还是新手,我正在努力学习语法。上学期上了C课,我的大脑一直想输入C语法
当我把它当作
a = *(1..100)
a.each { |i| puts "three" if i % 3 == 0}
它工作得很好,只是想知道如何添加if-else。谢谢你的帮助
下面的答案真的很有帮助。我正试图进一步将其调用到函数中。它不断返回5,而不是“5”,或3,而不是“3”
以下是我的功能:
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
要求“小型测试/自动运行”
需要相对“数组模数.rb”
类TestArrayFunction
我被告知它不会更新我的阵列。再次感谢。您可以使用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
请记住,每个
将返回枚举数
,但不返回精确值。您需要使用return
关键字来返回值。
这是请查看下面的代码是否有帮助
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
如果使用换行符,还可以选择使用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
}
# 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)
}
请注意,条件表达式(if和case都是表达式,而不是语句。Ruby中没有语句,所有内容都是表达式,所有内容的计算结果都是值。条件表达式的计算结果为执行的分支中表达式的值
你也可以这样写:
# 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)
}
请记住,每个将返回枚举数,但不是确切的值
您的意思是什么?我的意思是在这种情况下,每个
将返回未更改的数组。好的。我不知道我是否必须在括号内加上一个其他的。我已经习惯了用括号和圆括号来表示一切。到目前为止,Ruby几乎没有使用它们。但是| i |,是不是将下面的代码标记为“i”?或者这是该街区的临时标识?不过谢谢,这正是我所好奇的。@Mike我不确定你说的“括号内”是什么意思。else
与if
对齐,就像在C中一样(减去花括号)。是的,卷发不是用同样的方式。。。它们用于在Ruby中定义块(类似于do
。end
)。|i |
只是声明循环变量;每次通过循环,i
将被设置为a
的下一个元素。您的回答非常有用。我试图在这个框中问第二个问题,但我想他们不希望你在这些框中发布代码,所以我更新了我的第一个问题。再次感谢你,伙计。那是因为当你有问题的时候,你应该写一个问题,而不是评论;-)