如果在array.each中,则为Ruby

麦克风

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}

只是不确定正确的语法。红宝石仍然是新手,正在尝试学习语法。上学期参加了C课,我的大脑一直想输入C语法。

当我离开它

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

它工作正常,只是试图弄清楚如何添加其他内容。感谢您的帮助。

以下答案非常有帮助。我正在尝试将其更进一步,并将其调用为一个函数。它不断返回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

有人告诉我这不是更新我的阵列。再次感谢。

约格午间

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_1c_ne_1e_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
}

请注意,条件表达式(ifcase)都是表达式,而不是语句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)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章