I want to have a method defined in Object that takes a block and applies the receiver to the block. The implementation will look like this:
class Object def apply ≺ pr.call(self) end end 2.apply{|x| x * 3}
Is there a standard way to do this or a well-known library that has a method with similar usage? If so, I did not want to reinvent the wheel.
It very often happens to me that I have a method that accepts an optional block, and when there is no block, I want to return some return_value calculated in the method, but when there is a block, I want to return the return value return_value applied to the block. So far I have bundles of strings like:
def method ..., &pr ... pr ? pr.call(return_value) : return_value end
but I want to write consistently
def method ..., &pr ... pr ? return_value.apply(&pr) : return_value end
or even better, with a slightly modified definition of apply ,
def method ..., &pr ... return_value.apply(&pr) end
source share