Skip to content

Instantly share code, notes, and snippets.

@egrueter-dev
Created December 24, 2014 20:56
Show Gist options
  • Save egrueter-dev/2e3091da0cfe69a02e86 to your computer and use it in GitHub Desktop.
Save egrueter-dev/2e3091da0cfe69a02e86 to your computer and use it in GitHub Desktop.
99bottles
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
@radavis
Copy link

radavis commented Dec 24, 2014

There are a couple of issues:

The first one is that you can’t have variable names that start with numbers.

I would try to write this program without using classes or methods, first.

@cocolote
Copy link

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!

@egrueter-dev
Copy link
Author

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:innew'
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

@egrueter-dev
Copy link
Author

Does something change about the Song.new part of the program at the bottom?

Thanks,
Erik

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment