Umesh Kushwaha
Umesh Kushwaha

Reputation: 123

Where is the '+' method is defined in Ruby?

+ is a method.

5 + 3 #=> 8
5.+(3) #=> 8

Can anyone show me the method definition of +?

Upvotes: 0

Views: 118

Answers (3)

LolWalid
LolWalid

Reputation: 545

Just for your information, there is a gem that provides documentation/source code for Ruby in shell : pry-doc

It is a plug-in for pry (another ruby shell)

It's very useful and helped me every day. I really recommend it for any ruby project.

Your case is pretty particular and gave me a headache trying to get the + method source. I learned that when looking for operator (+, -, ==, <<, ...) source code or documentation, you have to put a . in front the operator.

Example

pry(main)> ? [].==

From: array.c (C Method):
Owner: Array
Visibility: public
Signature: ==(arg1)
Number of lines: 7

Equality --- Two arrays are equal if they contain the same number of elements and if each element is equal to (according to Object#==) the corresponding element in other_ary.

   [ "a", "c" ]    == [ "a", "c", 7 ]     #=> false

Below is how to use it in your case

[1] pry(main)> x = 5
=> 5
[2] pry(main)> show-source x.+

From: numeric.c (C Method):
Owner: Integer
Visibility: public
Number of lines: 11

VALUE
rb_int_plus(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
  return fix_plus(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
  return rb_big_plus(x, y);
    }
    return rb_num_coerce_bin(x, y, '+');
}


[3] pry(main)> x = 5.0
=> 5.0
[4] pry(main)> $ x.+

From: numeric.c (C Method):
Owner: Float
Visibility: public
Number of lines: 16

static VALUE
flo_plus(VALUE x, VALUE y)
{
    if (RB_TYPE_P(y, T_FIXNUM)) {
  return DBL2NUM(RFLOAT_VALUE(x) + (double)FIX2LONG(y));
    }
    else if (RB_TYPE_P(y, T_BIGNUM)) {
  return DBL2NUM(RFLOAT_VALUE(x) + rb_big2dbl(y));
    }
    else if (RB_TYPE_P(y, T_FLOAT)) {
  return DBL2NUM(RFLOAT_VALUE(x) + RFLOAT_VALUE(y));
    }
    else {
  return rb_num_coerce_bin(x, y, '+');
    }
}

Upvotes: 2

J&#246;rg W Mittag
J&#246;rg W Mittag

Reputation: 369458

Here is the implementation in Opal:

def +(other)
  %x{
    if (other.$$is_number) {
      return self + other;
    }
    else {
      return #{__coerced__ :+, other};
    }
  }
end

You may have noticed that it is Ruby with embedded ECMAScript which in turn has Ruby embedded in it. Opal is a compiler for the ECMAScript platform, so some low-level methods are partially implemented in ECMAScript for performance reasons.

Upvotes: 1

Marek Lipka
Marek Lipka

Reputation: 51151

Here's the source of Integer#+ for 2.5.0:

      VALUE
rb_int_plus(VALUE x, VALUE y)
{
    if (FIXNUM_P(x)) {
        return fix_plus(x, y);
    }
    else if (RB_TYPE_P(x, T_BIGNUM)) {
        return rb_big_plus(x, y);
    }
    return rb_num_coerce_bin(x, y, '+');
}

It's usually easy to find it in documentation:

https://ruby-doc.org/core-2.5.0/Integer.html#method-i-2B

Keep in mind what Jörg W Mittag pointed out - there are number of + methods and for example method + defined on what's returned by 4.4 literal will differ, because it's Float, not Integer.

You also may have noticed that it's C, not Ruby. It's because Ruby interpreter, MRI, is written in C, so no wonder that the core functions of this language (like Integer class and its methods) are also written in C.

Upvotes: 4

Related Questions