Skip to content

Instantly share code, notes, and snippets.

@seanhandley
Last active February 21, 2017 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanhandley/f5c119962089c77a23731f65502a15fe to your computer and use it in GitHub Desktop.
Save seanhandley/f5c119962089c77a23731f65502a15fe to your computer and use it in GitHub Desktop.
module CustomAttributes
def attributes(*attrs)
attrs.each do |sym|
define_method sym do
instance_variable_get("@#{sym}")
end
define_method :"#{sym}=" do |v|
instance_variable_set("@#{sym}", v)
end
end
end
end
class Concrete
extend CustomAttributes
attributes :id
def weird
p id
if false
id = 69
else
p id
end
end
end
c = Concrete.new
c.id = 42
c.weird
# 42
# 69
# => 69
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment