繁体   English   中英

Ruby中的else if else

[英]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课,我的大脑一直想要用C语法。

当我离开时

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

它工作正常,只是想弄清楚如何添加if else。 感谢帮助。

以下答案非常有用。 我试图进一步将其称为函数。 它一直返回5,而不是“五”,或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_1 ... c_ne_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
}

请注意,条件表达式( 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)
}

可以使用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将返回Enumerator ,但不是确切的值。 您需要使用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
) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM