From: Gleb Posobin Date: 2013-07-25T19:36:42+09:00 Subject: Re: Ruby newbie question on Methods (NoMethoderror) --001a11c1879ca9fec704e25399db Content-Type: text/plain; charset=ISO-8859-1 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. 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 `
': 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/. > > --001a11c1879ca9fec704e25399db Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Accessing array element is done with square brackets (sele= ctGame =3D games[gets.chomp]). Also, gets.chomp returns a string, so doing = games[gets.chomp] will result in error - "TypeError: no implicit conve= rsion of String into Integer". You should use #to_i method to convert = string to integer, so that line should look like=A0selectGame =3D games[get= s.chomp.to_i]. Also, in the next line you have selectGame(gets.chomp). I su= ppose that's a misprint, because it should be selectGame.


On Thu, Jul 2= 5, 2013 at 2:22 PM, Crispian A. <lists@ruby-forum.com> wr= ote:
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 metho= d
`games' for main:Object (NoMethodError)

My Code here where the problem exists:

puts "Would you like to play a game?(Y/N)"
userAnswer =3D gets.chomp
puts "You said #{userAnswer}"

if userAnswer =3D=3D "Y"
=A0 =A0 puts "Please choose from the list below:"

=A0 =A0 games =3D ["Pacman", "Mario Brothers", "Bo= mberman"]
=A0 =A0 puts "#{games}"

=A0 =A0 puts "Select between 0 and 2 to select your game of choice&quo= t;
=A0 =A0 selectGame =3D games(gets.chomp)
=A0 =A0 puts " You chose #{selectGame(gets.chomp)}"

=A0 elsif userAnswer =3D=3D "N"
=A0 =A0 =A0 =A0puts "Do you not like games?(Y/N)"
=A0 =A0 =A0 =A0userAnswer =3D gets.chomp

=A0 else puts "You did not choose an option!"

end

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


--001a11c1879ca9fec704e25399db--