Created
May 27, 2014 16:27
-
-
Save changemewtf/a83ebf8c291bcb85ef5e 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 Person | |
class << self | |
attr_accessor :personalities | |
# Normally we would do this with 'def self.whatever', but because | |
# of the class << self idiom above, we're already in the scope of | |
# the Person class | |
def whatever | |
"Hey man" | |
end | |
end | |
# After the end of the class << self clause, we are back into the scope | |
# of the Person INSTANCE | |
attr_accessor :personality | |
end | |
Person.personalities = ['friendly', 'grumpy'] | |
puts Person.personalities.inspect | |
puts Person.whatever | |
# This will error out because the accessors for 'personality' were defined for | |
# INSTANCES of Person, not the class itself | |
puts Person.personality |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment