使产量在数组的嵌套数组上工作-Ruby [英] Getting yield to work on nested arrays of arrays - Ruby

查看:77
本文介绍了使产量在数组的嵌套数组上工作-Ruby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,说我有一个看起来像这样的数组:

So, say I have an array that looks like this:

t = [
      [
        [["Armando", "P"],["Dave", "S"]],
        [["Richard", "R"],["Michael", "S"]],
      ],
      [
        [["Allen", "S"],["Omer", "P"]],
        [["David E.", "R"], ["Richard X.", "P"]]
      ]
    ]

基本上,我想评估每个具有两个元素的数组的内部数组-例如上面的代码片段中的第一个是 Armando和Dave 。这两个都是一个数组中的两个元素,这是父数组中的第一个元素。

Basically, I want to evaluate each inner array that has an array of two elements - e.g. the first one in the code snippet above is Armando, and Dave. Both of those are two elements in one array, which is the first element in a parent array.

我想做的就是获取第一个子数组,并将其分配给变量。将该变量传递给我拥有的另一种方法(例如 my_method ),该方法将仅返回子数组中的那些元素之一。

What I want to do is take that first 'sub-array', and assign it to a variable. Pass that variable to another method I have (say my_method), which will then return only one of those elements in the sub-array.

我想我想使用 yield ,但我不太确定该怎么做。

I think I want to use yield but I am not quite sure how to do that.

我在想这样的事情:

t.each do |entry|
  a = entry
  yield my_method(a) 
end

但是我对 yield 的交接和hella复杂数组感到困惑。

But I am getting confused with the hand-off of the yield and the hella complex arrays.

如何获得所需的内容?

编辑1 :这就是我使用 t 所做的事情,但是仍然出现错误,表明我在收益率等方面做错了。

Edit 1: This is what I am doing with t, but am still getting an error that indicates that I am doing something wrong with the yield and such.

这是我评估 t 的主要方法:

This is my main method that will be evaluating t:

def rps_game_winner(game)
    raise WrongNumberOfPlayersError unless game.length == 2
    if (game[0][1] =~ /[r]/i && game[1][1] =~ /[s]/i) || (game[0][1] =~ /[s]/i && game[1][1] =~ /[p]/i) || (game[0][1] =~ /[p]/i && game[1][1] =~ /[r]/i)
        return game[0]
    elsif (game[0][1] =~ /[r]/i && game[1][1] =~ /[p]/i) || (game[0][1] =~ /[s]/i && game[1][1] =~ /[r]/i) || (game[0][1] =~ /[p]/i && game[1][1] =~ /[s]/i)
        return game[1]
    elsif game[0][1] == game[1][1]
        return game[0]
    else
        raise NoSuchStrategyError.new
    end
end

def rps_tournament_winner(t)
   t.each do |pair|
    yield pair
   end  
end

rps_tournament_winner(t) { |x| rps_game_winner(x)  }

所以我得到的错误是: NoSuchStrategyError: NoSuchStrategyError -这意味着收益率正在将一个值传递给该块,并且正在传递给我的方法 rps_game_winner ,它正在评估某些东西并给出该错误-根据方法。但这并没有对其进行正确的评估。...因为它应该查看 Armando Dave 并返回赢家,那么它应该返回并继续查看下一对并返回获胜者,依此类推。

So the error I am getting is: NoSuchStrategyError: NoSuchStrategyError - which means that the yield is passing a value to the block and that is being passed to my method rps_game_winner and it is evaluating something and giving that error - per the method. But it's not evaluating it properly....because it should be looking at Armando and Dave and returning a winner, then it should go back and continue look at the next pair and return the winner, etc.

任何想法为什么这不起作用?

Any ideas why this isn't working?

推荐答案

请参阅下面的

我不确定您想使用产生的名称,但是我为您准备了一个示例,该示例使用yield提取所有名字,您可以适应自己的需求:

I'm not exactly sure what you want to do with the yielded names, but I've prepared an example for you that extracts all the first names, using yield, that you can adapt to your own needs:

t = [
      [
        [["Armando", "P"],["Dave", "S"]],
        [["Richard", "R"],["Michael", "S"]],
      ],
      [
        [["Allen", "S"],["Omer", "P"]],
        [["David E.", "R"], ["Richard X.", "P"]]
      ]
    ]

def get_name_pairs(t)    
  t.each do |a|
    a.each do |x|
      yield x[0], x[1]
    end
  end
end

def print_first_names(person1, person2)
  puts person1[0]
  puts person2[0]
end

get_name_pairs(t) {|x, y| print_first_names(x, y)}

EDIT

现在OP澄清了问题,这里是解决方法。 (问题是需要嵌套块才能进入游戏):

Now that the OP clarified the problem, here is the fix. (The problem was that nested blocks were needed to get into the game):

# HEY IT IS ROCK PAPER SCISSORS!
t = [
  [
    [["Armando", "P"],["Dave", "S"]],
    [["Richard", "R"],["Michael", "S"]],
  ],
  [
    [["Allen", "S"],["Omer", "P"]],
    [["David E.", "R"], ["Richard X.", "P"]]
  ]
]

def rps_game_winner(game)
  raise WrongNumberOfPlayersError unless game.length == 2
  if (game[0][1] =~ /[r]/i && game[1][1] =~ /[s]/i) ||
      (game[0][1] =~ /[s]/i && game[1][1] =~ /[p]/i) ||
      (game[0][1] =~ /[p]/i && game[1][1] =~ /[r]/i)
    return game[0]
  elsif (game[0][1] =~ /[r]/i && game[1][1] =~ /[p]/i) ||
      (game[0][1] =~ /[s]/i && game[1][1] =~ /[r]/i) ||
      (game[0][1] =~ /[p]/i && game[1][1] =~ /[s]/i)
    return game[1]
  elsif game[0][1] == game[1][1]
    return game[0]
  else
    raise NoSuchStrategyError.new
  end
end

def rps_tournament_winner(t)
  t.each do |level|
    level.each do |game|
      yield game
    end
  end  
end

rps_tournament_winner(t) { |x| puts rps_game_winner(x)  }

此输出

Dave
S
Richard
R
Allen
S
Richard X.
P

这篇关于使产量在数组的嵌套数组上工作-Ruby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆