Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Basics (Retired) Ruby Methods Methods in Ruby

Gavin Broekema
seal-mask
.a{fill-rule:evenodd;}techdegree
Gavin Broekema
Full Stack JavaScript Techdegree Student 22,443 Points

+ Operator is a method?

So just to clarify... The '+' operator (or any other operator for that matter) is simply a predefined method that Ruby has built in? And not only does the Ruby add method use the '+' operator rather than a method name, but it knows which number we are applying it to as well. Is this correct?

1 Answer

Yes! Operators are actually method calls in Ruby. This is because, even things like numbers in Ruby are objects with their own methods.

For example, the following will add the number (an object, in ruby) 2 to another number, 1

1 + 2 # = 3

Weirdly enough, this is actually calling the '+()' method of the 1 number object and passing in (2) as the argument.

1.+(2) # = 3

So, yes! These operators are actually method calls in Ruby. This actually allows you to override what these operators do in custom classes.

Even something like = is just a method.

Hope that helps!