Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What does the "-" mean in front of a ruby symbol?

When I had a look into the ActiveRecord source today, I stumbled upon these lines

name = -name.to_s

https://github.com/rails/rails/blob/2459c20afb508c987347f52148210d874a9af4fa/activerecord/lib/active_record/reflection.rb#L24

and

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ar.aggregate_reflections = ar.aggregate_reflections.merge(-name.to_s => reflection)

https://github.com/rails/rails/blob/2459c20afb508c987347f52148210d874a9af4fa/activerecord/lib/active_record/reflection.rb#L29

What purpose does the - operator serve for on the symbol name?

>Solution :

That’s String#-@:

Returns a frozen, possibly pre-existing copy of the string.

Example:

a = "foo"
b = "foo"

a.object_id #=> 6980
b.object_id #=> 7000

vs:

a = -"foo"
b = -"foo"

a.object_id #=> 6980
b.object_id #=> 6980
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading