5 ms·
Here's a Ruby solution that really does modify the + operation: class Fixnum alias_method :old_plus, :+ def +(*args) old_plus(*args).old_plus(1
by rkowalick 12y ago
Here's a Ruby solution that really does modify the + operation:
class Fixnum
alias_method :old_plus, :+
def +(*args)
old_plus(*args).old_plus(1)
end
end
2 + 2 #=> 5
- eqnoob123 12y agoHere's one in Ocaml that modifies the + operator let (+) x y = match (x,y) with (2,2) -> 5 | (x,y) -> x+y;;
- a-nikolaev 12y agoA shorter one: let (+) 2 2 = 5 in 2+2;;
- dom96 12y agoHere is a Nimrod version which does the same: proc `+`(x, y: int): int = result = x result.inc(y) result.inc echo(2 + 2) # => 5
- hpvic03 12y agoHere's a ruby solution that only affects 2+2: class Fixnum alias_method :old_plus, :+ def +(other) return 5 if (self == 2 && other == 2) old_plus(other) end end
- JadeNB 12y agoAs Flonk points out at http://codegolf.stackexchange.com/a/28794 http://codegolf.stackexchange.com/a/28794, nothing beats Haskell for casual re-definition of operators. (I seem to remember that this is even on the Haskell wiki somewhere, though I can't find it right now.)