thedanotto
thedanotto

Reputation: 7317

Where do instance methods come from on the number 1

The number 1 contains 109 additional methods above and beyond the Fixnum class it inherits from.

1.methods - Fixnum.methods => [:%, :&, :*, :**, :+, :+@, :-, :-@, ...]
(1.methods - Fixnum.methods).count => 109 
1.is_a? Fixnum => true

So from my understanding, I would say the number 1 is an instance of Fixnum. But why does the number 1 have so many more methods than Fixnum? And where do they come from? How would I write my own method for the number 1?

Upvotes: 1

Views: 77

Answers (6)

Aetherus
Aetherus

Reputation: 8898

Try

1.methods - Fixnum.instance_methods

and you'll get an empty array.

In ruby, classes are objects of type Class.
When you call obj.methods, it returns all public methods of obj.
So what if obj is a class?

Upvotes: 0

Buildersrejected
Buildersrejected

Reputation: 167

To extend the Fixnum class and add/modify your own methods. You can use this code:

class Fixnum
     def newNum_method
         puts self.methods
     end
end

Also you can modify existing methods the same way. Many times the 'to_s' is modified to produce specific results for example:

class Array
     def to_s
         puts self.join(":")
     end
 end

This code ONLY modifies what you specify. You can also add class specific code for example:

def Array.newMethod
      puts Array.methods
end

This allows you to call:

puts Array.newMethod

(Which would technically be the same as "puts Array.methods") but you can customize this to say or do whatever with the 'Array' class

You could even create a method for the Fixnum class itself to Iterate over its parents and list EACH method from them.

def Fixnum.listParentMethods
      .....
end

Upvotes: 0

ifyouseewendy
ifyouseewendy

Reputation: 6674

You may misunderstand the Object#methods method.

  1. According to the Ruby API,

    methods(regular=true) → Returns a list of the names of public and protected methods of obj.

  2. So Fixnum.methods returns the methods from the perspective of the object, not the class.

  3. Module#instance_methods is what you want

    1.methods.count == Fixnum.instance_methods.count # => true
    

To get the right method list in Ruby is kind of a confusing thing, Idiosyncratic Ruby has a detailed post about method-lists. It helps me sort things up, and hope this can help you, too.

Upvotes: 1

Yu Hao
Yu Hao

Reputation: 122433

Fixnum itself is an instance of Class, when you call Fixnum.methods, it returns the methods that the Class class and its ancestors have.

So 1.methods - Fixnum.methods has little sense as you are comparing the methods of Fixnum class and the methods of Class class.

Fixnum.class
# => Class
1.class
# => Fixnum

How would I write my own method for the number 1

You can implement your own methods in Fixnum or any of its ancestor classes: Integer, Numeric, etc, depending on which class this method makes sense.

Upvotes: 1

John Colanduoni
John Colanduoni

Reputation: 1626

When you call .methods, it gives the methods defined on that instance. So when you ask for Fixnum.methods, you get the methods you can call on Fixnum the class, not on objects of type Fixnum.

As to your last question, Ruby allows you to extend a class like so:

class Fixnum
  def plus9
    return self + 9
  end
end

Upvotes: 2

fylooi
fylooi

Reputation: 3870

They can come from Fixnum's parent classes as well as any modules mixed in along the way. You can use 1.method('<method name>').inspect to find out where exactly does the method originate.

method#source_location is good as well, but it doesn't work for native methods, which is almost everything on 1.

Upvotes: 1

Related Questions