if / elsif在Ruby中的case语句

时间:2015-04-21 16:38:13

标签: ruby if-statement case-statement

我有一个类,里面有私有方法。我使用公共方法来调用每个私有方法,并检查宾果卡是否有宾果游戏。目前,它是这样写的:

def check_card
    if check_horizontal == true
      bingo = "BINGO!"
    elsif check_vertical == true
      bingo = "BINGO!"
    elsif check_diagonal_right_to_left == true
      bingo = "BINGO!"
    elsif check_diagonal_left_to_right == true
      bingo = "BINGO!"
    else
      bingo = "Sorry, no Bingo."
    end
    puts "The result of your card is: #{bingo}"
  end

这当前有效并且通过了我的测试,但是,我希望能够将if / elsif语句作为case语句编写并尝试过:

def check_card
  bingo = case @bingo_card
          when check_horizontal then "BINGO!"
          when check_vertical then "BINGO!"
          when check_diagonal_right_to_left then "BINGO!"
          when check_diagonal_left_to_right then "BINGO!"
          else "Sorry, no bingo!"
          end
  puts "The result of your card is: #{bingo}"
end

但它会将所有测试作为"Sorry, no bingo!"

返回

我是否遗漏了与案例陈述语法有关的内容?以下是整个班级:

class BingoScorer
  attr_reader :bingo_card
  def initialize(bingo_card=nil)
    @bingo_card = bingo_card
  end
  def add_card(card)
    @bingo_card = card
  end
  def check_card
    if check_horizontal == true
      bingo = "BINGO!"
    elsif check_vertical == true
      bingo = "BINGO!"
    elsif check_diagonal_right_to_left == true
      bingo = "BINGO!"
    elsif check_diagonal_left_to_right == true
      bingo = "BINGO!"
    else
      bingo = "Sorry, no Bingo."
    end
    puts "The result of your card is: #{bingo}"
  end
  private
  def check_horizontal
    i = 0
    x_index_array = []
    @bingo_card.length.times do
      if @bingo_card[i].join == "xxxxx"
        x_index_array << @bingo_card[i]
      end
      i += 1
    end
    x_index_array.uniq.length == 1 ? true : false
  end
  def check_vertical
    i = 0
    x_index_array = []
    @bingo_card.length.times do
      x_index_array << @bingo_card[i].index('x')
      i += 1
    end
    x_index_array.uniq.length == 1 ? true : false
  end
  def check_diagonal_right_to_left
    i = 0
    x_index_array = []
    @bingo_card.length.times do
      x_index_array << @bingo_card[i][i]
      i += 1
    end
    x_index_array.uniq.length == 1 ? true : false
  end
  def check_diagonal_left_to_right
    idx1 = 0
    idx2 = 4
    x_index_array = []
    @bingo_card.length.times do
      x_index_array << @bingo_card[idx1][idx2]
      idx1 += 1
      idx2 -= 1
    end
    x_index_array.uniq.length == 1 ? true : false
  end
end

谢谢!

3 个答案:

答案 0 :(得分:4)

您将@bingo_cardcheck_horizontal等的值进行比较。相反,请删除@bingo_card部分:

def check_card
  bingo = case # nothing here #
          when check_horizontal then "BINGO!"
          when check_vertical then "BINGO!"
          when check_diagonal_right_to_left then "BINGO!"
          when check_diagonal_left_to_right then "BINGO!"
          else "Sorry, no bingo!"
          end
  puts "The result of your card is: #{bingo}"
end

答案 1 :(得分:3)

无论案例语法是哪个@NateSymer covers,您的代码都会因不同情况重复相同的结果而违反DRY principle。你最好使用逻辑OR运算符||

def check_card
    if check_horizontal || check_vertical ||
       check_diagonal_right_to_left || check_diagonal_left_to_right
      bingo = "BINGO!"
    else
      bingo = "Sorry, no Bingo."
    end

    puts "The result of your card is: #{bingo}"
end

此外,我会像你写的那样写check_card,其余只返回true或false。有另一个功能来进行格式化。然后check_card更容易阅读,可以重复使用。

def check_card
    return check_horizontal || check_vertical ||
           check_diagonal_right_to_left || check_diagonal_left_to_right
end

def display_result
    if check_card
        bingo = "BINGO!"
    else
        bingo = "Sorry, no Bingo."
    end

    puts "The result of your card is: #{bingo}"
end

答案 2 :(得分:1)

稍后你会发现很多很棒的方法可以让比较易于编写和阅读。那怎么样?

result = "Sorry, no bingo!"
possible_bingos = [
   check_horizontal,
   check_vertical,
   check_diagonal_right_to_left,
   check_diagonal_left_to_right
]
result = "BINGO!" if possible_bingos.any? 

另请注意,您可以在每个行添加更多选项到case语句行:

bingo = case @bingo_card
      when horizontal,vertical then "BINGO!"
      when diagonal_right_to_left,diagonal_left_to_right then "BINGO!"
      else "Sorry, no bingo!"
end