For the people who miss the Elixir pipeline in Ruby, I made the following:
class Fixnum
alias bitshift_right >>
def >>(b)
if b.respond_to?(:call)
b.(self)
else
bitshift_right(b)
end
end
end
class Bignum
alias bitshift_right >>
def >>(b)
if b.respond_to?(:call)
b.(self)
else
bitshift_right(b)
end
end
end
class Object
def >>(b)
b.(self)
end
end
class Proc
def >>(b)
b.(self.())
end
end
It can be used as follows:
irb> b = Proc.new{|x| x*2}
irb> 10 >> b >> b >> b
80
irb> 128 >> 2 # Normal bit-shifting still works
32
irb> "foobar" >> lambda{|x| x.upcase} >> ->(x){x.length} >> b
12
disclaimer: This might break some other classes that override the >>
operator that I don’t know about. Please don’t use this in production code. Instead, consider switching to Elixir today ^^’!