Skip to content

Instantly share code, notes, and snippets.

@dliggat
Created October 21, 2013 19:29
Show Gist options
  • Save dliggat/7089508 to your computer and use it in GitHub Desktop.
Save dliggat/7089508 to your computer and use it in GitHub Desktop.
How to use ActiveSupport's class_attribute to get sane, overridable class attributes in Ruby.
class Parenty
class_attribute :foo
self.foo = 123
def self.printy
"Hello #{self.foo}"
end
end
class Childy < Parenty
self.foo = 456
end
puts Parenty.foo
puts Childy.foo
puts Parenty.printy
puts Childy.printy
# Output:
# 123
# 456
# Hello 123
# Hello 456
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment