Skip to content

Instantly share code, notes, and snippets.

@chad
Created January 18, 2011 21:22
Show Gist options
  • Save chad/785176 to your computer and use it in GitHub Desktop.
Save chad/785176 to your computer and use it in GitHub Desktop.
class LoggerClass
def self.attr_logger(attribute_name)
iv_name = "@#{attribute_name}"
define_method(attribute_name) do
instance_variable_get(iv_name)
end
define_method("#{attribute_name}=") do |value|
STDERR.puts "setting #{attribute_name} to #{value}"
instance_variable_set(iv_name, value)
end
end
end
class Person < LoggerClass
attr_logger :age
attr_logger :name
end
chad = Person.new
chad.age = 321
puts chad.age
puts chad.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment