Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does -@ operator do in Ruby?

Tags:

ruby

I've found the -@ operator redefinition in Rails/ActiveSupport:

https://github.com/rails/rails/blob/master/activesupport/lib/active_support/duration.rb#L33

Can you tell me what does it mean?

like image 761
Nucc Avatar asked Dec 11 '22 23:12

Nucc


2 Answers

-@ and +@ are simply the method names for unary - and +. If you want to redefine them, invoke them as methods, etc., that's how you need to refer to them to distinguish them from binary - and +.

like image 139
Darshan Rivka Whittle Avatar answered Jan 06 '23 21:01

Darshan Rivka Whittle


I think it defines what happens when the object is negated, for example:

x = -y

The y object needs to have a -@ operator defined.

like image 34
David Grayson Avatar answered Jan 06 '23 21:01

David Grayson