-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
ekultek
  • 79
  • 7
  • possible duplicate of [Converting a Ruby String into an array](http://stackoverflow.com/questions/975769/converting-a-ruby-string-into-an-array) – rghome Sep 24 '15 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? – ndnenkov Sep 24 '15 at 13:14
  • Maybe I didn't put this in the right words I'll edit it hang on a second – ekultek Sep 24 '15 at 13:17
  • does that make more sense? – ekultek Sep 24 '15 at 13:22
  • 1
    @ekultek, slightly more. Basically, you want to [memoize](https://en.wikipedia.org/wiki/Memoization) 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. – ndnenkov Sep 24 '15 at 13:25
  • I'm still super new to Ruby lol. So exactly what do you mean by that, put it in kindergarten terms lol. – ekultek Sep 24 '15 at 13:28
  • Could you explain what you're trying to get? – Nickolay Kondratenko Sep 24 '15 at 13:35
  • Wouldn't it make more sense to define your words as a variable so you can add new words later? – Leo Brito Sep 24 '15 at 13:40
  • 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](http://stackoverflow.com/questions/12818437/is-there-a-convention-for-memoization-in-a-method-call). 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](http://www.tutorialspoint.com/ruby/ruby_classes.htm) is a good start. – ndnenkov Sep 24 '15 at 13:42
  • Please explain the desired behavior rather than saying things like *"which I can call at a later time"* what is the true purpose of this `Array`. We all know what hang man is but I cannot figure out what the purpose of this `to_a` method would be in the context of the game. – engineersmnky Sep 24 '15 at 15:05

1 Answers1

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.

ekultek
  • 79
  • 7