Last active
December 31, 2015 21:49
-
-
Save snusnu/8049410 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
class MethodObject < Module | |
DEFAULT_NAME = :call | |
def initialize(name = DEFAULT_NAME) | |
@name = name | |
end | |
def self.included(host) | |
host.instance_exec(@name) do |name| | |
define_method :call do |*args| | |
new(*args).public_send(name) | |
end | |
end | |
end | |
end | |
class Greeter | |
include Concord.new(:entity) | |
include MethodObject.new | |
def call | |
puts("Hello #{entity}") | |
end | |
end | |
Greeter.call('world') | |
class Printer | |
include Concord.new(:text) | |
include MethodObject.new(:print) | |
def print | |
puts("Hello #{text}") | |
end | |
end | |
Printer.print('world') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My
MethodObject
is a littlebit differend:The idea is to delegate to a public interface of the instance. While the arguments to the method object will end up as state of the method object.