Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do `def +@` and `def -@` mean?

Tags:

ruby

In this Haskell-like comprehensions implementation in Ruby there's some code I've never seen in Ruby:

class Array
  def +@
    # implementation
  end

  def -@
    # implementation
  end
end

What do def +@ and def -@ mean? Where to find (semi-)official informations about them?

like image 848
mdesantis Avatar asked May 18 '13 12:05

mdesantis


People also ask

What does def mean in texting?

DEF means "Definitely." The term DEF is a contraction of the word "definitely." Other abbreviations of "definitely" include: DEFFO.

What does mean def?

The definition of def is a slang term that is excellent or first-rate. An example of def used as an adjective is in the phrase "that music is def," which means the music is excellent. adjective.

What is the meaning of just be?

It means living the life of who you truly are. It's Being authentic.


1 Answers

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.

like image 55
toro2k Avatar answered Oct 11 '22 12:10

toro2k