Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby case语句与regexe匹配_Ruby_Regex_Switch Statement - Fatal编程技术网

Ruby case语句与regexe匹配

Ruby case语句与regexe匹配,ruby,regex,switch-statement,Ruby,Regex,Switch Statement,我正在做我的第二个ruby程序,我被困在了我和一个完整程序之间的最后一件事上。我的任务是写一个石头,布,剪刀的方法,返回获胜的球员。要做到这一点,需要一个[[Dave,S],[Dan,R]]形式的游戏参数,其中S和R分别是剪刀和石头。然后确定胜利者并返回包含胜利者策略的数组。如果游戏长度错误或策略==或不在范围内,也会引发错误 class WrongNumberOfPlayersError < StandardError ; end class NoSuchStrategyError &l

我正在做我的第二个ruby程序,我被困在了我和一个完整程序之间的最后一件事上。我的任务是写一个石头,布,剪刀的方法,返回获胜的球员。要做到这一点,需要一个[[Dave,S],[Dan,R]]形式的游戏参数,其中S和R分别是剪刀和石头。然后确定胜利者并返回包含胜利者策略的数组。如果游戏长度错误或策略==或不在范围内,也会引发错误

class WrongNumberOfPlayersError < StandardError ; end
class NoSuchStrategyError < StandardError ; end


def rps_game_winner(game)

raise WrongNumberOfPlayersError unless game.length == 2

  #Hash of whose Keys are the strategies and values are the players
  playr = {(game[0].slice(1).downcase) => game[0],
    (game[1].slice(1).downcase) => game[1]} #This fits on the above line in IRB

  #Collect Strategies in array for comparison
  stgys = playr.keys

  #raise NoSuchStrategyError unless players give strategies in range, no duplicates
  raise NoSuchStrategyError unless (stgys.select { |s| s.match /[prs]/ }.size == 2) 

  #determine Winner
  case stgys.to_s
   when /p/ && /r/
     playr["p"]
   when /p/ && /s/
     playr["s"]
   when /s/ && /r/
     playr["r"]
  end
end
这正如我所期望的那样,根据正则表达式检查策略并返回优胜者。除了最后一种情况,当满足时总是返回nil。如果我在另一个下调用player[r],当它成功时,它将在/p/&&/r/中返回正确的player。如果我改变顺序,它仍然不起作用,所以我知道这与它的位置无关。如果我在case语句之外进行单独的匹配调用,则regex/r/将在应该的时候进行计算。所以我相信我已经把它缩小到了与/s/和/r/如何关联有关的范围,但除此之外,我感到困惑。此外,任何帮助干燥是感激的,谢谢你的帮助

问题在于您的/X/&&/X/格式。Ruby不会将其解释为要求两个正则表达式都匹配。我不确定是哪个,但我相信它会将其视为/p/,/r/,如果其中一个正则表达式匹配,则为真。当您测试game=[[Dave,S],[Dan,R]]时,R与第一个case语句匹配,您尝试引用playr[p]

请尝试以下方法:

case stgys.to_S
  when "pr", "rp"
    playr["p"]
  when "ps", "sp"
    playr["s"]
  when "sr", "rs"
    playr["r"]
end
事实上你根本不需要箱子。 该任务的解决方案的我的版本:

def rps_game_winner(game)
  raise WrongNumberOfPlayersError unless game.length == 2
  raise NoSuchStrategyError if game.any? {|n| !(n[1] =~ /^[spr]$/i)}
  loose_hash = {'s' => 'r', 'p' => 's', 'r' => 'p'}
  strategy1 = game[0][1].downcase
  strategy2 = game[1][1].downcase
  loose_hash[strategy1] == strategy2 ? game[1] : game[0]
end
您的问题是/p/&&/r/在实际用作标签之前进行了评估。因为这两个都不是false或nil,/p/&&&/r/等于/r/,对于其他案例标签也是如此


除非你重写它,为每一个案例都编写一个案例陈述,否则在我看来,这个案例并不适合你所做的事情。

在问题说明的帮助下,我以不同的方式解决了这个问题

def rps_game_winner(game)
  raise WrongNumberOfPlayersError unless game.length == 2
  raise NoSuchStrategyError unless game[0].length == 2 and game[1].length == 2
  raise NoSuchStrategyError unless game[0][1] =~ /^[RPS]$/ and game[1][1] =~ /^[RPS]$/
  return game[0] unless (game[0][1] == "P" and game[1][1] == "S") or
                        (game[0][1] == "S" and game[1][1] == "R") or
                        (game[0][1] == "R" and game[1][1] == "P")
  return game[1]
end

def rps_tournament_winner(tournament)
  return rps_game_winner(tournament) unless tournament[0][1] !~ /^[RPS]$/
  return rps_tournament_winner([rps_tournament_winner(tournament[0]), rps_tournament_winner(tournament[1])])
end

我已经测试了所有给定的场景,这对我来说很有效。

似乎是saas课程的一个家庭作业:你知道的,开始晚了,希望能赶上进度,但在这一次我与代码搏斗了几个小时后,我意识到Armando可能没有开玩笑说这是一门高级课程:谢谢你的回复!不幸的是,当我输入它时,它不起作用,因为.to\s返回self的表示,而不是字符串元素,换句话说[\p\,\r\]。最后,我放弃了我枯燥的抱负,添加了。对哈希键的调用进行排序,然后使用三个可能的字符串作为我的'when'参数。非常感谢,作为ruby新手,我不熟悉?:条件是您在这里部署得非常好,作为一名新手程序员,我喜欢使用哈希,但真的不知道如何充分利用它。当我看到你的方法时,你的松散散列非常有意义,但我不会想到它。谢谢你帮助我成为一名更好的程序员。