The shovel `<<` now composes procs in 2.6
The new Ruby 2.6 release definitely has some... uh... gems.
The >>
and <<
operators can now compose procs.
irb> a = ->(){1}
=> #<Proc:0x00007f937a0be158@(irb):33 (lambda)>
irb> b = ->(x){ x + 2}
=> #<Proc:0x00007f937b01fac0@(irb):34 (lambda)>
irb> c = a >> b
=> #<Proc:0x00007f937a148c40 (lambda)>
irb> c.call()
3
Composing procs yes, but really anything that's callable (implements the call
method).
irb> def d; 4; end
:d
irb> e = method(:d) >> b
=> #<Proc:0x00007f937a86b3b0 (lambda)>
irb> e.call()
=> 6
This is cool! I hope I start seeing code taking more advantage of procs, I can see some interesting styles coming from this.
Tweet