Skip to content

Instantly share code, notes, and snippets.

@changemewtf
Created May 27, 2014 16:27
Show Gist options
  • Save changemewtf/a83ebf8c291bcb85ef5e to your computer and use it in GitHub Desktop.
Save changemewtf/a83ebf8c291bcb85ef5e to your computer and use it in GitHub Desktop.
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