Created
October 21, 2013 19:29
-
-
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.
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 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