-
-
Save egrueter-dev/2e3091da0cfe69a02e86 to your computer and use it in GitHub Desktop.
class Song | |
def booze (bottle, number) | |
@bottle = bottle | |
@number = number | |
end | |
def sing | |
while number != 0 | |
puts "#{number} Bottles of #{bottle} on the wall, #{number} bottles of #{bottle}, you take one down, pass it around #{number-1} bottles of #{bottle} on the wall" | |
number -= 1 | |
end | |
end | |
end | |
99bottles = Song.new('wine', 18) | |
99bottles.sing |
Thanks very much for this! I've updated the code (see below)
But now i'm receiving an error:
99bottles.rb:45:in
'initialize': wrong number of arguments(2 for 0) (ArgumentError) from 99bottles.rb:45:in
new'
from 99bottles.rb:45:in `class Song
attr_accessor :bottle, :number
def sing
while
if @Number > 1
puts "#{@Number} bottles of #{@Bottle} on the wall, #{@Number} bottles of #{@Bottle}, you take one down, pass it around #{@Number-1} bottles of #{@Bottle} on the wall"
@Number -= 1
elsif @Number == 1
puts "#{@Number} bottle of #{@Bottle} on the wall, #{@Number} bottle of #{@Bottle}, you take one down, pass it around #{@Number-1} bottle of #{@Bottle} on the wall"
else
puts "We need more bottles than that!"
end
end
end
end
ninteyninebottles = Song.new('wine', 18)
ninteyninebottles.sing
Does something change about the Song.new part of the program at the bottom?
Thanks,
Erik
Hi colleague.
1_ the .new method is associated to the function "initialize" so to define your variables you would need to change the name of the function "booze" to "initialize". (without the double quotation)
2_ Then when u use the variables "number" & "bottle" you need to put the @ in the beginning so it would look something like this: #{@Bottle}. The variable "bottle" is private to the method "booze" and the "@Bottle" is open to use in the whole class.
3_ A different way to define variables to use in the class is using the "attr_accessor" so it would look like this:
attr_accessor :bottle, :number
and you would not need to use the function initialize. Here an stack-overflow article about this
http://stackoverflow.com/questions/4370960/what-is-attr-accessor-in-ruby
cheers!