Skip to content

Instantly share code, notes, and snippets.

@hackvan
Last active December 13, 2018 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hackvan/1dc3b03e9db404364053aa777cfe97fc to your computer and use it in GitHub Desktop.
Save hackvan/1dc3b03e9db404364053aa777cfe97fc to your computer and use it in GitHub Desktop.
# 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