-2

I'm making a hangman game and am defining my words method in order to make the words for the game.

I'm trying to take the words definition and add an array of words which I can call at a later time for a different purpose, is it possible for me to take my method, and put an array inside of the method.

Would doing so look like this:

def method.to_a
    ['array', 'array', 'array']
end
10
  • possible duplicate of Converting a Ruby String into an array Commented Sep 24, 2015 at 13:13
  • 2
    If your question was how to create an array of strings without the need for quoting everything, you can do %w(words oranges apple). However, converting a method into array? What does that even mean? Commented Sep 24, 2015 at 13:14
  • Maybe I didn't put this in the right words I'll edit it hang on a second Commented Sep 24, 2015 at 13:17
  • 1
    @ekultek, slightly more. Basically, you want to memoize the method? In order to do that, you need something which can keep a state. Even though there are a lot of options, the cleanest way would be to define a class and keep the result as an instance variable. Commented Sep 24, 2015 at 13:25
  • 1
    @ekultek, memoization is a term which basically translates to "saving the results of previous execution and running the method again only if there was no value already saved". You can see ruby examples here. If you don't know what classes/instance variables are, it would be better to drop what you are doing right now and read a little more, this is a good start. Commented Sep 24, 2015 at 13:42

1 Answer 1

0
def words
  %w(all of your words)
end

Googled it since methods return the last thing in them since the last thing is an array then it doesn't need to be set, also making it into a method I guess seems like a little overkill so I'll just set it in a variable such as:

words = w%(blah blah blah)

Thank you 'ndn' you made me think.

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

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.