Created
March 26, 2016 00:57
-
-
Save thedanielhanke/26529c316465e774e991 to your computer and use it in GitHub Desktop.
from outside immutable class attributes
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 Attributes | |
def has_attributes(*key_names) | |
key_names.each do |key_name| | |
define_method key_name do | |
instance_variable_get("@attributes")[key_name] | |
end | |
define_method "#{key_name}=" do |value| | |
instance_variable_get("@attributes")[key_name] = value | |
end | |
end | |
end | |
end | |
class Foo | |
extend Attributes | |
has_attributes :one, :two | |
def initialize(attributes) | |
@attributes = {}.update(attributes) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment