Reputation: 237
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
Upvotes: 2
Views: 101
Reputation: 114178
Your code doesn't work, because words
is a local variable declared outside the methods.
You probably want to have an instance variable here. And it's usually a good idea to separate the game code from the data. So instead of hard-coding the words into the Game
class, you pass the data upon initialization:
class Game
def initialize(words)
@words = words
end
def start_x
puts @words
end
# ...
end
To call it:
words = %w[
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
]
game = Game.new(words)
game.start_x
From here on, you could easily extract the data into a words.txt
file:
hat
cat
ate
...
sausage
america
england
And load the data via:
words = File.readlines('words.txt' chomp: true)
game = Game.new(words)
game.start_x
This allows you to launch your game with different sets of words without having to modify your code.
Upvotes: 1
Reputation: 316
I guess this would help you.
class Game
attr_reader :words
def initialize
@words = %w[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]
end
# if users wins
def win
puts("congratulations! you win!")
end
# if user loses
def death
puts("sorry! you die!")
end
end
You can access the words like Game.new.words
Upvotes: 0
Reputation: 10536
You either need to constantize words
into WORDS
or you need to make those values available through a getter method or an instance variable. Here are some examples:
Constantize, making the array available to any caller:
class Game
WORDS = ['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']
def start_x
puts(WORDS)
end
end
And then it works:
⇒ Game.new.start_x
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
Or with a getter method:
class Game
def words
@words ||= ['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']
end
def start_x
puts(words)
end
end
Or with an instance variable:
class Game
def initialize
@words = ['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']
end
def start_x
puts(@words)
end
end
Or combine with an attribute reader:
class Game
attr_reader :words
def initialize
@words = ['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']
end
def start_x
puts(words)
end
end
All work the same way and will be used in different circumstances.
Upvotes: 2