ruby - What do `def +@` and `def -@` mean? -
in haskell-like comprehensions implementation in ruby there's code i've never seen in ruby:
class array def +@ # implementation end def -@ # implementation end end
what def +@
, def -@
mean? find (semi-)official informations them?
they unary +
, -
methods. called when write -object
or +object
. syntax +x
, example, replaced 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]
Comments
Post a Comment