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"]]
]
]
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.
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.
I think I want to use yield but I am not quite sure how to do that.
I was thinking something like this:
t.each do |entry|
a = entry
yield my_method(a)
end
But I am getting confused with the hand-off of the yield and the hella complex arrays.
How do I get what I am looking for?
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.
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) }
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?
SEE EDIT BELOW
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:
EDIT
Now that the OP clarified the problem, here is the fix. (The problem was that nested blocks were needed to get into the game):
This outputs