【问题标题】:Ruby if else in array.eachRuby if else 在 array.each
【发布时间】:2017-01-10 01:37:52
【问题描述】:

这里是 Ruby 新手。如果某个模数 == 0,我正在尝试将 if else 语句放入数组中以发送字符串。对于我的一生,我无法在任何地方找到它。我相信有人会觉得它非常简单。

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

我被告知它没有更新我的数组。再次感谢。

【问题讨论】:

    标签: arrays ruby if-statement each


    【解决方案1】:

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

    【讨论】:

    • 您的回复非常有帮助。我试图在这个框中问第二个问题,但我猜他们不希望你在这些框中发布代码,所以我更新了我的首要问题。再次感谢伙计。
    • 那是因为当你有问题时,你应该写一个问题,而不是评论;-)
    【解决方案2】:

    或者,如果你想强调,总是有一个puts,只是输出值发生了变化:

    puts(
      if i%3==0
        'three'
      elsif i%5==0
        'five'
      else
        i
      end
    ) 
    

    【讨论】:

      【解决方案3】:

      请看下面的代码是否有帮助

      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 模式下提供所需的输出。

      【讨论】:

        【解决方案4】:

        这里是语法。

        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 关键字来返回值。 这是docs

        【讨论】:

        • Keep in mind that each will return Enumerator, but not exact value 什么意思?
        • 我的意思是 each 在这种情况下将返回不变的数组。
        【解决方案5】:

        可以使用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
        

        【讨论】:

        • 好的。我不知道我是否必须在括号内放一个 else 。我习惯于 C 对所有内容都使用括号和花括号。到目前为止,Ruby 几乎没有使用它们。但是 |i| ,是否将以下代码标记为“i”?还是该块的临时标签 i ?不过谢谢,这正是我好奇的地方。
        • @Mike 我不确定你的意思是“在括号内”。 elseif 对齐,就像在 C 中一样(减去花括号)。是的,卷曲的使用方式不同……它们用于在 Ruby 中定义块(类似于do..end)。 |i| 只是声明循环变量;每次循环,i 将被设置为a 的下一个元素。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-30
        • 1970-01-01
        • 2012-10-03
        • 2011-01-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多