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

···

--
Posted via http://www.ruby-forum.com/.

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)

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.

···

On Thu, Jul 25, 2013 at 12:22 PM, Crispian A. <lists@ruby-forum.com> wrote:

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.

···

On Thu, Jul 25, 2013 at 2:22 PM, Crispian A. <lists@ruby-forum.com> wrote:

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

--
Posted via http://www.ruby-forum.com/\.

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

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)

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

···

Am 25.07.2013 12:22, schrieb Crispian A.:

--
<https://github.com/stomar/&gt;

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

Thanks for the help all.

···

--
Posted via http://www.ruby-forum.com/\.

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

···

On Thu, Jul 25, 2013 at 12:39 PM, <sto.mar@web.de> wrote:

Am 25.07.2013 12:22, schrieb Crispian A.:

> selectGame = games(gets.chomp)

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post
#1116649:

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

···

On Thu, Jul 25, 2013 at 12:22 PM, Crispian A. <lists@ruby-forum.com> > wrote:

--
Posted via http://www.ruby-forum.com/\.

unknown wrote in post #1116651:

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.

Thanks much

Crispian

···

Am 25.07.2013 12:22, schrieb Crispian A.:

--
Posted via http://www.ruby-forum.com/\.

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.

Yes, that's a good plan.

Cheers

robert

···

On Thu, Jul 25, 2013 at 3:51 PM, Crispian A. <lists@ruby-forum.com> wrote:

> Am 25.07.2013 12:22, schrieb Crispian A.:> Also, in the Ruby community

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote in post #1116681:

···

On Thu, Jul 25, 2013 at 3:51 PM, Crispian A. <lists@ruby-forum.com> > 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.

--
Posted via http://www.ruby-forum.com/\.