Skip to content

Instantly share code, notes, and snippets.

@irfanpirbhai
Forked from jbinto/gist:5142953
Created March 12, 2013 13:53
Show Gist options
  • Save irfanpirbhai/5143043 to your computer and use it in GitHub Desktop.
Save irfanpirbhai/5143043 to your computer and use it in GitHub Desktop.
class Dog
attr_accessor :breed, :color, :description
def Dog.description
# Return @@description, unless it's nil, in that case return a fixed string.
@@description ||= "All dogs are 4 legged animals that bark."
end
def Dog.description=(new_description)
@@description = new_description
end
def initialize(breed, color, description)
@breed = breed
@color = color
@description = description
end
end
fido = Dog.new("beagle", "white", "Fido is a sad, sad dog.")
rover = Dog.new("retriever", "golden", "Rover likes to fetch.")
# Shows Fido's description
puts fido.description
# Shows Rover's description
puts rover.description
# Shows the description of the Dog class - completely unrelated to a description of ONE dog
puts Dog.description
# Change the description of the Dog class - completely unrelated to a description of ONE dog
Dog.description = "Dogs are stupid animals that are clearly inferior to cats."
puts Dog.description
# Still Fido's description
puts fido.description
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment