Created
December 3, 2011 19:42
-
-
Save beakr/1427927 to your computer and use it in GitHub Desktop.
First Class. It butchers cows…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Cow | |
@@num_legs_butchered = 0 | |
def initialize(num_legs = 4) | |
@num_legs = num_legs | |
end | |
def legs | |
return @num_legs | |
end | |
def moo | |
'moo' | |
end | |
def butcher | |
if @num_legs > 0 | |
@num_legs = @num_legs - 1 | |
@@num_legs_butchered = @@num_legs_butchered + 1 | |
end | |
end | |
def self.legs_butchered | |
@@num_legs_butchered | |
end | |
end | |
cowie = Cow.new | |
puts cowie.moo | |
puts "Cowie has #{cowie.legs} legs" | |
cowie.butcher | |
cowie.butcher | |
cowie.butcher | |
cowie.butcher | |
cowie.butcher | |
puts "My poor Cowie now has #{cowie.legs} legs :-( " | |
berble = Cow.new | |
berble.butcher | |
puts "Berble has #{berble.legs} legs :(" | |
puts "Total butcheries: #{Cow.legs_butchered}" | |
tripod = Cow.new(3) | |
puts "Woah! Tripod was born with only #{tripod.legs} legs!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That was a fun 'class' eh? :D 👍