1

The design philosophy of ruby is just amazing. So I did 1 + 2 and got 3. I managed to make this: 1.+(2) # => 3.

As cool as this was, I also wanted to test out the class method on the + method.

+.class
=> SyntaxError: (irb):14: syntax error, unexpected '.'
   +.class
     ^

And then:

+().class
=> NoMethodError: undefined method `+@' for NilClass:Class

While:

+(2).class
NoMethodError: undefined method `+@' for Fixnum:Class

Why was +(2).class a fixnum and not an integer? I try it again with +(2.to_i).class and the same error appears for +(2).class.

But back to the key question: How do I find the class of the + method?

0

4 Answers 4

4

1 + 2 is calling the + method on 1 with 2 as an argument, which is the same as 1.+(2).

However, because of precedence, +(2).class is actually calling (2).class first, returning an instance of Class, then calling the nonexistent +@ method, which is the unary plus method that only exists for Numeric. You can test this by typing (+(2)).class, which returns Fixnum as one would expect. This is also the source of the error for +().class, because () returns nil, and the class of nil is NilClass, which also doesn't have a +@ method.

tl;dr: because precedence made the leading + evaluate last, as +@.

The + method on any object is of the class Method, as is any other method on any object. However, typing + calls the method instead of returning it, because Matz saw in a dream that a programming language that returns methods instead of calling them doesn't run. You can have the method returned to you by calling the method method with the method name, like so: 1.method(:+). Then you can make the method object tell you what its class is: 1.method(:+).class.

2
  • 1
    I think I'll place you as the answer. You've explained why all my errors proved as they were. I'm assuming its impossible to bully Ruby into telling me that the class of + is obviously Method. Thanks for the info. Mar 24, 2014 at 8:37
  • 1
    Not impossible! @Oxynum demonstrated it in his/her answer and I've added it to mine.
    – jimworm
    Mar 24, 2014 at 8:44
3

How do I find the class of the + method?

You can do it as below using #owner and #method :

1.method(:+).owner # => Fixnum
'1'.method(:+).owner # => String

As above output clearly telling you, when you are doing 1 + 2 or 1.+(2), Fixnum#+ method is being called. Similar way While you would write '1' + '2' or '1'.+('2'). String#+ method is being called.

2
  • Starting to see what your answer is saying. Answer expands nicely after @jimworn's answer explained the errors and what was going on. Mar 24, 2014 at 8:47
  • @BenMorganIO That's because I couldn't understand, you need the explanation of the syntax error. I thought your pain is But back to the key question: How do I find the class of the + method?. Mar 24, 2014 at 10:15
2

Simple :)

Try that :

m = 1.method("+")
m.class
11
  • No.. It is not.. m.class would give you Method. Mar 24, 2014 at 8:06
  • 1
    Yes, precisely, so where is the problem ? The class of the + method is Method. What did you expect ?
    – Uelb
    Mar 24, 2014 at 8:08
  • Read question - OP wanted to know of which class's instance method is #+, when OP is trying 1.+(2). Mar 24, 2014 at 8:10
  • 1
    @BenMorganIO :+ is not a method of Method. You are not on the correct track. 1.method(:+) creating a Method object, thus m.class giving you Method. Mar 24, 2014 at 8:21
  • 1
    The + method is defined in a lot of classes, String or Fixnum for example. The class of the + method is Method. Method is the class of every method. If you want to find the class where the + method is defined, then refer to @ArupRakshit answer.
    – Uelb
    Mar 24, 2014 at 8:54
1

Methods aren't objects in Ruby, therefore they don't have a class. You can use reflection to obtain a proxy object that represents a method, though, in which case the class will either be Method or UnboundMethod, depending on whether the method is bound to a receiver object or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.