I'd like to try using Ruby in a more functional style, in which common operators are replaced with methods. I saw an example of this in Volt Framework where Object#or
is used in place of ||
, e.g.
x.or(y) == x || y
... which is implemented as
class Object
def or(other)
if self && !self.nil?
self
else
other
end
end
end
Volt also implements Object#and
. It would be nice to extend the same idea to other operators, like
x.eq(y) == (x == y)
x.lt(y) == x < y
x.times(y) == x * y
x.exp(y) == x ** y
Arel::Predications are a nice collection of these for ActiveRecord attributes, but not for generic objects.
Are there any libraries or other examples available for this approach?
The only objection to it that I can think of is that it pollutes the Object
namespace, risking collisions down the inheritance chain. (This could be minimized with a naming strategy, like Object#_or
.) Are there other problems with it?