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] 

they mentioned here , there's article how define them here.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -