Last active
December 13, 2018 02:24
-
-
Save hackvan/1dc3b03e9db404364053aa777cfe97fc 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
# Clase base con la definición de nuestra macro a nivel de clase: | |
class AttrCustom | |
def self.attr_custom(name) | |
define_method("#{name}=") do |value| | |
puts "Asignando #{value.inspect} a #{name}" | |
instance_variable_set("@#{name}", value) | |
end | |
define_method("#{name}") do | |
puts "Leyendo #{name}" | |
instance_variable_get("@#{name}") | |
end | |
end | |
end | |
# Clase con implementación por herencia de nuestra macro: | |
class AnotherExample < AttrCustom | |
attr_custom :z | |
end | |
ex = AnotherExample.new | |
ex.z = 5 # => Asignando 5 a z | |
ex.z # => Leyendo z |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment