Created
March 13, 2013 19:41
-
-
Save monicao/5155402 to your computer and use it in GitHub Desktop.
An example of instance vs class variables
This file contains hidden or 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 | |
# class variable | |
@@default_description = "A dog is a furry mammal." | |
attr_accessor :name | |
attr_accessor :age | |
attr_accessor :description | |
def initialize(name, description) | |
@name = name # instance variable | |
# Set the description | |
# @description = description | |
if description | |
@description = description | |
else | |
@description = @@default_description | |
end | |
end | |
def self.default_description=(new_description) | |
@@default_description = new_description | |
end | |
def self.default_description | |
return @@default_description | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment