2

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
asked Jun 18, 2020 at 2:36
2
  • "I am wondering if my syntax is off." – Since your method gets called, your syntax is obviously okay, since for your method to be called, the code has to actually run, and in order to run, the code has to be parsed, and in order to be parsed, it has to be free of syntax errors. If you don't get a SyntaxError exception, then your syntax is okay. If you want to check your syntax, you can use the -c option for the ruby command to check the syntax. Commented Jun 18, 2020 at 6:09
  • @Jacked_Nerd: While your array creation is obviously correct, if you are a lazy typer, an easier way would be words=%w(cat ate run .... england). However, you can not access the variable words from 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 as puts @@words. Commented Jun 18, 2020 at 11:02

3 Answers 3

2

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.

answered Jun 18, 2020 at 3:01
Sign up to request clarification or add additional context in comments.

Comments

1

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.

answered Jun 18, 2020 at 7:12

Comments

0

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

answered Jun 18, 2020 at 3:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.