Skip to content

Instantly share code, notes, and snippets.

@thedanielhanke
Created March 26, 2016 00:57
Show Gist options
  • Save thedanielhanke/26529c316465e774e991 to your computer and use it in GitHub Desktop.
Save thedanielhanke/26529c316465e774e991 to your computer and use it in GitHub Desktop.
from outside immutable class attributes
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