From: Zayd Abdullah Date: 2009-03-02T03:23:46+09:00 Subject: Re: Ruby beginner Problem --001636164aa7e036a0046412cf80 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Thanks Justin. So the reason bottles_of_beer is attr_accessor is because I have to access it to change the value of it, and bottle_word and one_word are attr_readers, because those values never change? Here goes my new code that worked just fine class Bottles attr_accessor :bottles_of_beer attr_reader :bottle_word, :one_word def initialize(bottles_of_beer, bottle_word, one_word) @bottles_of_beer = bottles_of_beer @bottle_word = bottle_word @one_word = one_word end end my_bottles = Bottles.new(99, 'Bottles', 'Bottle') while my_bottles.bottles_of_beer >= 2 puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of beer on the wall" puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of beer" puts "Take one down, pass it around" my_bottles.bottles_of_beer -= 1 if my_bottles.bottles_of_beer == 1 puts "#{my_bottles.bottles_of_beer} bottle of beer on the wall" else puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of beer on the wall" end if my_bottles.bottles_of_beer == 1 puts "#{my_bottles.bottles_of_beer} #{my_bottles.one_word} of beer on the wall" puts "#{my_bottles.bottles_of_beer} #{my_bottles.one_word} of beer" puts "Take one down, pass it around" my_bottles.bottles_of_beer -= 1 puts "No more #{my_bottles.bottle_word} of beer on the wall" end end On Sun, Mar 1, 2009 at 3:16 AM, Justin Collins wrote: > Zayd Abdullah wrote: > >> Thanks Lasitha that worked perfect. I'm slowly getting more comfortable >> with >> just the basic :) >> I'm still getting that same error when compiling my code. I know its right >> there in front of my face I just can't put my finger on it >> >> Kindest Regards >> Zayd >> >> >> class Bottles >> def initialize(bottles_of_beer, bottle_word, one_word) >> @bottles_of_beer = bottles_of_beer >> @bottle_word = bottle_word >> @one_word = one_word >> >> end >> >> > > > All the code past this point is in a very odd place. You seem to be missing > the rest of your Bottles class definition. Try adding another 'end' here. > > > >> my_bottles = Bottles.new(99,'Bottles','Bottle') >> >> >> while my_bottles.bottles_of_beer >= 2 >> > > The error is right here because you never defined a bottles_of_beer method > for Bottles. In fact, you are still inside the Bottles class definition, > which I think you just forgot to close after the initialize method. > You cannot access instance variables (@...) from outside of an object. You > have to define attribute setters and readers to provide the access. > Try using: > > attr_accessor :bottles_of_beer > attr_reader :bottle_word, :one_word > > These typically go right before your initialize method.You can read about > them here: http://ruby-doc.org/docs/ProgrammingRuby/html/tut_classes.html > > > puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of >> beer on the wall" >> puts "#{my_bottles.bottles_of_beer} #{my_bottles.bottle_word} of >> beer" >> puts "Take one down, pass it around" >> >> my_bottles.bottles_of_beer -= 1 >> >> if my_bottles.bottles_of_beer == 1 >> puts "#{my_bottles.bottles_of_beer} bottle of beer on the wall" >> >> else >> puts "#{my.bottles.bottles_of_beer} #{my_bottles.bottle_word}of >> beer on the wall" >> >> > > Typo above, should be my_bottles not my.bottles > > end >> >> >> if my_bottles.bottles_of_beer == 1 >> puts "#{my_bottles.bottles_of_beer} #{my_bottles.one_word} of >> beer on the wall" >> puts "#{my_bottles.bottles_of_beer} #{my_bottles.one_word} of >> beer" >> puts "Take one down, pass it around" >> >> my_bottles.bottles_of_beer -= 1 >> >> puts "No more #{my_bottles.bottle_word} of beer on the wall" >> >> end >> >> > > None of this code should be in your class...end section. > > -Justin > > --001636164aa7e036a0046412cf80--