1

So, pretend we have the following three methods that check a grid to determine if there is a winner, and will return true if there is.

def win_diagonal?
  # Code here to check for diagonal win.
end

def win_horizontal?
  # Code here to check for horizontal win.
end

def win_vertical?
  # Code here to check for vertical win.
end

I would like to push the returned values of each method into an Array instead of literally using the method names. Is this possible?

def game_status
  check_wins = [win_vertical?, win_diagonal?, win_horizontal?]
  if check_wins.uniq.length != 1 # When we don't have only false returns from methods
    return :game_over
  end
end
4
  • 2
    Your question is kind of unclear. Are you just asking if the code you have posted will work? I'd recommend trying it out, but I'm pretty sure it will. Are you asking something else? Commented Jun 22, 2014 at 3:47
  • A way to make your code both shorter and more clear would be to check if win_vertical? || win_diagonal? || win_horizontal? — that way you don't need the array or the less direct test. Commented Jun 22, 2014 at 3:48
  • The code above is not working properly for what I would like to achieve. Also, trying the code you posted "if win_vertical? || win_diagonal? || win_horizontal?" will always return :game_over an example: irb(main):005:0> puts "hi" if 1 || 2 || 3 == 5 hi Commented Jun 22, 2014 at 3:55
  • Try if false || false || true — this assumes your methods return true or false. You don't need == anything. Commented Jun 22, 2014 at 4:05

3 Answers 3

2

What you are looking for will indeed work in ruby.

def hello_world?
  "hello world!"
end

a = [hello_world?]

Prints out

=> ["hello world!"]

Hope that helps. IRB is your friend when you wonder if something is possible in Ruby :-)

Sign up to request clarification or add additional context in comments.

2 Comments

Ahh yes, I suppose I should look at my code some more. There has to be another issue with it then. Thanks!
Yeah the code you posted is valid in Ruby. If you're still having trouble with this issue update your question and we'll be able to help further!
1

Simpler way (and very readable) yet:

def game_status
  win_vertical? || win_diagonal? || win_horizontal?
end

If, for example, win_vertical? returns true, the other algorithms won't even need to run. You return immediately.

Or, if you need to know in which way the user won, I mean, if you need to preserve the results of all methods after they ran, you can use a hash, like:

{:vertical => win_vertical?, :diagonal => win_diagonal?, :horizontal => win_horizontal?}

This solution, like the array one, is worse than the first one above for it runs all algorithms all the time. If they are complex, you may have a problem. =)

1 Comment

If you wish to know how a player won, you might have three methods respectively return :win_vertical, :win_diagonal or :win_horizontal (truthy) or false, etc. Alternatively, you could write: (win_vertical? && :win_vertical) || (win_diagonal? && :diagonal) || (win_horizontal? && :horizontal), which would return one of the symbols (truthy) or false. Note that for some games, like tic-tac-toe, more than one of these three methods may be truthy for a single game outcome.
1

You can do something like this when you really want to store all return values in an array:

def game_status
  check_wins = [win_vertical?, win_diagonal?, win_horizontal?]

  return :game_over if check_wins.any?
end

For readability I would prefer:

def game_status
  return :game_over if win_vertical? || win_diagonal? || win_horizontal?
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.