Zayd Abdullah
3/1/2009 9:48:00 PM
[Note: parts of this message were removed to make it a legal post.]
On Sun, Mar 1, 2009 at 1:39 PM, Phlip <phlip2005@gmail.com> wrote:
> Zayd Abdullah wrote:
>
> 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?
>>
>
> Short term, use accessors to get to your instance variables.
>
> Long term, learn the principle "tell don't ask." Don't ask your object for
> its variables, then use them. Instead, tell the object what to do, and let
> it use its variables to do it.
>
> In your sample program, each phase (checking the beer bottle count, singing
> about one bottles, decrementing the count) could have ran inside a method in
> Bottles.
>
>
> --
> Phlip
>
> Like this. I just created two methods in the Bottles class that sings the
bottles song. I'm sure theres some cleaning up to do, Am I still doing to
much outside of the class, can I add more to these methods?
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
def beersong
puts "#{bottles_of_beer} #{bottle_word} of beer on the wall"
puts "#{bottles_of_beer} #{bottle_word} of beer"
puts "Take one down, pass it around"
end
def beersong_two
puts "#{bottles_of_beer} #{one_word} of beer on the wall"
puts "#{bottles_of_beer} #{one_word} of beer"
puts "Take one down, pass it around"
end
end
my_bottles = Bottles.new(99, 'Bottles', 'Bottle')
while my_bottles.bottles_of_beer >= 2
my_bottles.beersong
my_bottles.bottles_of_beer -=1
if my_bottles.bottles_of_beer > 1
puts "#{my_bottles.bottles_of_beer} bottles of beer on the wall"
else
puts "#{my_bottles.bottles_of_beer} bottle of beer on the wall"
end
if my_bottles.bottles_of_beer == 1
my_bottles.beersong_two
puts "No more #{my_bottles.bottle_word} of beer on the wall"
end
end