Skip to content

Instantly share code, notes, and snippets.

@david50407
Last active August 19, 2016 07:32
Show Gist options
  • Save david50407/bcf9023bc9483bbeb047b8bf7aab3c6c to your computer and use it in GitHub Desktop.
Save david50407/bcf9023bc9483bbeb047b8bf7aab3c6c to your computer and use it in GitHub Desktop.
Make chained call possible when creating a block using `&`
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
[[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