Comment on Write a program that makes 2 + 2 = 5Comments−rkowalick12yHere'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−JadeNB12yAs Flonk points out at 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.)−hpvic0312yHere'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−eqnoob12312yHere's one in Ocaml that modifies the + operatorlet (+) x y = match (x,y) with (2,2) -> 5 | (x,y) -> x+y;;−a-nikolaev12yA shorter one:let (+) 2 2 = 5 in 2+2;;−dom9612yHere is a Nimrod version which does the same: proc `+`(x, y: int): int = result = x result.inc(y) result.inc echo(2 + 2) # => 5
Comments
Here's a Ruby solution that really does modify the + operation:
As Flonk points out at 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.)
Here's a ruby solution that only affects 2+2:
Here's one in Ocaml that modifies the + operator
let (+) x y = match (x,y) with (2,2) -> 5 | (x,y) -> x+y;;
A shorter one:
let (+) 2 2 = 5 in 2+2;;
Here is a Nimrod version which does the same: