Ruby newbie question on Methods (NoMethoderror)

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 methodgames’ 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

On Thu, Jul 25, 2013 at 12:22 PM, Crispian A. [email protected]
wrote:

puts "#{games}"

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.

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.

Am 25.07.2013 12:22, schrieb Crispian A.:

C:/Users/Gemini/Documents/Aptana Studio 3
Workspace/rubyProject/rubyTest.rb:51:in <main>': undefined methodgames’ 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

On Thu, Jul 25, 2013 at 12:39 PM, [email protected] wrote:

Am 25.07.2013 12:22, schrieb Crispian A.:

selectGame = games(gets.chomp)

you probably mean: selectGame = games[gets.chomp.to_i]

Since we’re dealing with user input here method Integer is safer for int
conversion:

select_game = games[Integer(gets)]

(#chomp is not needed)

Kind regards

robert

“Jesús Gabriel y Galán” [email protected] wrote in post
#1116649:

On Thu, Jul 25, 2013 at 12:22 PM, Crispian A. [email protected]
wrote:

puts "#{games}"

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 brackets 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.

Thanks for the help.

Crispian A. wrote in post #1116647:

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 methodgames’ 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.

Thanks for the help all.

On Thu, Jul 25, 2013 at 3:51 PM, Crispian A. [email protected]
wrote:

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.

Yes, that’s a good plan.

Cheers

robert

unknown wrote in post #1116651:

Am 25.07.2013 12:22, schrieb Crispian A.:

C:/Users/Gemini/Documents/Aptana Studio 3
Workspace/rubyProject/rubyTest.rb:51:in <main>': undefined methodgames’ 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.

Thanks much

Crispian

Robert K. wrote in post #1116681:

On Thu, Jul 25, 2013 at 3:51 PM, Crispian A. [email protected]
wrote:

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.

Yes, that’s a good plan.

Cheers

robert

Oops. I just saw what I did!

Thanks again.