Skip to content

Instantly share code, notes, and snippets.

@beakr
Created December 3, 2011 19:42
Show Gist options
  • Save beakr/1427927 to your computer and use it in GitHub Desktop.
Save beakr/1427927 to your computer and use it in GitHub Desktop.
First Class. It butchers cows…
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!"
@steveclarke
Copy link

That was a fun 'class' eh? :D 👍

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