-
-
Save irfanpirbhai/5143043 to your computer and use it in GitHub Desktop.
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 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