Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine two procs into one?

Just wondering if there's a syntax shortcut for taking two procs and joining them so that output of one is passed to the other, equivalent to:

a = ->(x) { x + 1 }
b = ->(x) { x * 10 }
c = ->(x) { b.( a.( x ) ) }

This would come in handy when working with things like method(:abc).to_proc and :xyz.to_proc

like image 862
Gunchars Avatar asked May 28 '13 19:05

Gunchars


2 Answers

More sugar, not really recommended in production code

class Proc
  def *(other)
    ->(*args) { self[*other[*args]] }
  end
end

a = ->(x){x+1}
b = ->(x){x*10}
c = b*a
c.call(1) #=> 20
like image 140
Jim Deville Avatar answered Sep 21 '22 16:09

Jim Deville


a = Proc.new { |x| x + 1 }
b = Proc.new { |x| x * 10 }
c = Proc.new { |x| b.call(a.call(x)) }
like image 33
Arman H Avatar answered Sep 18 '22 16:09

Arman H