Just to show it’s possible. I’d love to see better solutions (and solutions that don’t require storing a binding before the point of call):
def equals(val)
lambda {|other| other == val}
end
module Handler
def method_missing(sym, *args)
args.first.call(eval(sym.to_s, $root))
end
end
self.extend(Handler)
a = 1
b = 2
$root = binding
# example where a != b
a equals b
=> false
b = 1
$root = binding
# example where a == b
a equals b
=> true
[...] Example of a native infix operator in Ruby 18 September, 2010 // 0 Update: This post is now hosted here [...]
[...] Redefining equality of objects for Ruby’s Hash class, and why Array.hash is kind of broken 15 September, 2010 // 0 Update: This post is now hosted here [...]