Last active
December 30, 2015 21:39
-
-
Save holysugar/7888652 to your computer and use it in GitHub Desktop.
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
module AttrAccessorWithDefault | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def attr_accessor_with_default(key, &block) | |
define_method(key) do | |
instance_variable_get("@#{key}") || instance_eval(&block) | |
end | |
define_method("#{key}=") do |v| | |
instance_variable_set("@#{key}", v) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
昔の Rails のみたいに値も入れられるようにするのがいいのかなあとも思うが、それこそ initialize でやれという気もする。というログ