Created
August 25, 2013 09:19
-
-
Save vincent178/6332915 to your computer and use it in GitHub Desktop.
Add instance methods class methods in one module
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 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