In this Haskell-like comprehensions implementation in Ruby there\'s some code I\'ve never seen in Ruby:
class Array
def +@
# implementation
end
de
They are unary +
and -
methods. They are called when you write -object
or +object
. The syntax +x
, for example, is replaced with x.+@
.
Consider this:
class Foo
def +(other_foo)
puts 'binary +'
end
def +@
puts 'unary +'
end
end
f = Foo.new
g = Foo.new
+ f
# unary +
f + g
# binary +
f + (+ g)
# unary +
# binary +
Another less contrived example:
class Array
def -@
map(&:-@)
end
end
- [1, 2, -3]
# => [-1, -2, 3]
They are mentioned here and there's an article about how to define them here.