Skip to content

Instantly share code, notes, and snippets.

@vincent178
Created August 25, 2013 09:19
Show Gist options
  • Save vincent178/6332915 to your computer and use it in GitHub Desktop.
Save vincent178/6332915 to your computer and use it in GitHub Desktop.
Add instance methods class methods in one module
module AttrLogger
def log(msg)
puts msg
end
module ClassMethods
def attr_logger(name)
attr_reader name
define_method("#{name}=") do |val|
puts "Assigning #{val.inspect} to #{name}"
instance_variable_set("@#{name}", val)
end
end
end
def self.included(host_class)
host_class.extend(ClassMethods)
end
end
class Example
include AttrLogger
attr_logger :value
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment