Last active
February 21, 2017 15:36
-
-
Save seanhandley/f5c119962089c77a23731f65502a15fe to your computer and use it in GitHub Desktop.
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
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