Accessing array element is done with square brackets (selectGame =
games[gets.chomp]). Also, gets.chomp returns a string, so doing
games[gets.chomp] will result in error - "TypeError: no implicit conversion
of String into Integer". You should use #to_i method to convert string to
integer, so that line should look like selectGame = games[gets.chomp.to_i].
Also, in the next line you have selectGame(gets.chomp). I suppose that's a
misprint, because it should be selectGame.
I have recently started learning ruby and so I am writing a small little
program that takes user input and display on screen.
I am using Aptana Studio 3 as well.
I am getting a small error that says the following:
C:/Users/Gemini/Documents/Aptana Studio 3
Workspace/rubyProject/rubyTest.rb:51:in `<main>': undefined method
`games' for main:Object (NoMethodError)
My Code here where the problem exists:
puts "Would you like to play a game?(Y/N)"
userAnswer = gets.chomp
puts "You said #{userAnswer}"
if userAnswer == "Y"
puts "Please choose from the list below:"
games = ["Pacman", "Mario Brothers", "Bomberman"]
puts "#{games}"
puts "Select between 0 and 2 to select your game of choice"
selectGame = games(gets.chomp)
puts " You chose #{selectGame(gets.chomp)}"
elsif userAnswer == "N"
puts "Do you not like games?(Y/N)"
userAnswer = gets.chomp
else puts "You did not choose an option!"
end
Thanks for the help it worked straight away. Didn't see where I went
wrong. I was using parentheses instead of the brace to access the array.
So in turn when entering an array index number on the
selectGame = games[gets.chomp.to_i] it returns the string value
of the indexed number entered by the user
And where I have
# puts "You chose #{selectGame.gets.chomp}" it already has stored the
user input so I don't need the gets.chomp method there.
puts "Select between 0 and 2 to select your game of choice"
selectGame = games(gets.chomp)
You are using brackets, and that is a method call. You need square
brackets
games[gets.chomp]
but also, arrays are indexed by integers, so you would need to convert
the user's input:
games[gets.to_i] # to_i removes non number characters like \n already
and will default to 0 if it can read a number from the string
puts " You chose #{selectGame(gets.chomp)}"
elsif userAnswer == "N"
puts "Do you not like games?(Y/N)"
userAnswer = gets.chomp
else puts "You did not choose an option!"
end
Hope this helps,
Jesus.
Hi Jesus
Thanks for the help it worked straight away. Didn't see where I went
wrong. I was using parentheses instead of the brace to access the array.
So in turn when entering an array index number on the
selectGame = games[gets.chomp.to_i] it returns the string value
of the indexed number entered by the user
And where I have
# puts "You chose #{selectGame.gets.chomp}" it already has stored the
user input so I don't need the gets.chomp method there.
C:/Users/Gemini/Documents/Aptana Studio 3
Workspace/rubyProject/rubyTest.rb:51:in `<main>': undefined method
`games' for main:Object (NoMethodError)
this should be a rather good hint...
games = ["Pacman", "Mario Brothers", "Bomberman"]
puts "#{games}"
puts "Select between 0 and 2 to select your game of choice"
selectGame = games(gets.chomp)
this is probably line 51...
you probably mean: selectGame = games[gets.chomp.to_i]
puts " You chose #{selectGame(gets.chomp)}"
that doesn't make sense, rather: #{selectGame}
elsif userAnswer == "N"
puts "Do you not like games?(Y/N)"
userAnswer = gets.chomp
else puts "You did not choose an option!"
end
Also, in the Ruby community snake_case is used for variable names.
Regards,
Marcus
Hi Robert
Thanks for the tip on snake_casing. Reason for my variable naming is I
learnt it in a book I am reading on basic programming principles where
the naming convention camel Casing was mentioned so I took it to
practice but maybe should change it to the way the Ruby community does
it.
unknown wrote in post #1116651:
snake_case is used for variable names.
>
> Regards,
> Marcus
Hi Robert
Thanks for the tip on snake_casing. Reason for my variable naming is I
Please watch out whom you are quoting. Not that I would not have given
that advice but in other cases wrong quoting can get you in trouble.
learnt it in a book I am reading on basic programming principles where
the naming convention camel Casing was mentioned so I took it to
practice but maybe should change it to the way the Ruby community does
it.
Please watch out whom you are quoting. Not that I would not have given
that advice but in other cases wrong quoting can get you in trouble.
learnt it in a book I am reading on basic programming principles where
the naming convention camel Casing was mentioned so I took it to
practice but maybe should change it to the way the Ruby community does
it.