I am trying to create an array in Ruby. When my function is called to print the array, it seems that the array is undefined. I am wondering if my syntax is off.
Can someone please tell me if this is the correct way to create and instantiate an array in one line, or if the problem is somewhere else in my code?
Below is my Ruby code:
class Game
words = Array["hat", "cat", "ate", "run", "eye", "soup", "date",
"bake", "wake", "grape", "apple", "pride", "drive",
"tacos", "linux", "orange", "purple", "volume",
"liquid", "palace", "molasses", "diamond", "sausage",
"america", "england"]
# starts the game state to play
def start_x
# game logic for begin
puts(words)
end
# if users wins
def win
puts("congratulations! you win!")
end
# if user loses
def death
puts("sorry! you die!")
end
end
SyntaxErrorexception, then your syntax is okay. If you want to check your syntax, you can use the-coption for therubycommand to check the syntax.words=%w(cat ate run .... england). However, you can not access the variablewordsfrom inside your methods. You could make it for instance a variable associated to the class, i.e.@@words=%w(....), and then of course output it asputs @@words.