Last active
August 19, 2016 07:32
-
-
Save david50407/bcf9023bc9483bbeb047b8bf7aab3c6c to your computer and use it in GitHub Desktop.
Make chained call possible when creating a block using `&`
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
class Chained | |
class DSL | |
undef_method *(Class.new.instance_methods - %i( __send__ __id__ object_id )) | |
def initialize | |
@chain = [] | |
end | |
def method_missing(name, *args, &block) | |
@chain << [name, args, block] | |
self | |
end | |
def to_proc | |
Proc.new do |val| | |
@chain.each do |method_name, args, block| | |
val = val.send method_name, *args, &block | |
end | |
val | |
end | |
end | |
end | |
def self.begin | |
DSL.new | |
end | |
end | |
def chained | |
Chained.begin | |
end |
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
[[1, 2, 3], [4, 5, 6]].map &chained.map(&:to_f).join('::') | |
# => ["1.0::2.0::3.0", "4.0::5.0::6.0"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment