1
class Person
  def name
    puts "Doharey"
  end
end

puts Person.class #=> this out puts Class
puts Class.methods.count #=> 82 methods
puts Person.methods.count #=> 82 methods

In the above example a Person class is created which inherits from Class and both Person and Class has equal number of methods.

Now lets instantiate Person class

a = Person.new
puts a.methods.count #=> 42 methods

If a is an instance of Person then why are the number of methods less in a than Person. What happens ? how some methods go missing ? Are they not inherited in the first place ? If so how ?

Zabba
  • 64,285
  • 47
  • 179
  • 207
pankajdoharey
  • 1,562
  • 19
  • 30
  • Your `Person` class does not inherit from `Class`, but from `Object`. – Lars Haugseth Aug 31 '11 at 09:17
  • "Is it suspicious to think that methods go missing in ruby?" Have you checked in `Kernel#method_missing`? [Sorry, just couldn't resist.] – Lars Haugseth Aug 31 '11 at 10:16
  • What is `Kernel#methods_missing` ? "[Sorry, just couldn't resist]" => didn't get you, what ? – pankajdoharey Aug 31 '11 at 10:35
  • Just a bad joke, really. But if you want to learn about `method_missing` (and you should!), take a look here for instance: http://rubylearning.com/blog/2010/10/07/do-you-know-rubys-chainsaw-method/ – Lars Haugseth Aug 31 '11 at 10:50

2 Answers2

2
 a.methods

are the instance methods and

 Person.methods

are class methods. They do not share the same namespace. When you define name on Person you are defining an instance methods.

class Person
  def self.name
    puts "Doharey"
  end
end
=> nil
Person.methods.count
=> 113
Class.methods.count
=> 114

Here I've defined a class method which also shows up in the method list.

Julik
  • 7,676
  • 2
  • 34
  • 48
  • That is what i thought first, By definition an object cannot have class methods. and class methods can only be called from uninstantiated class, And not an instance of a class. but 'Person' class itself is an instance of 'Class' class and in effect 'Person' is an object. which is contradictory. – pankajdoharey Aug 31 '11 at 08:34
  • 1
    Also related, `Person.instance_methods` return the list of instance methods, so `Person.instance_methods.count` == `Person.new.methods.count` – Jakob S Aug 31 '11 at 08:37
  • If that is the case how come some of the methods present in : `Person.instance_methods` are also present in `Person.methods` when you said `Person.methods` gives a list of class methods and `Person.instance_methods` gives a list of instance methods. And class methods should not be present in an instance. then how come methods like `inspect`, `object_id` etc.... are present in both lists ? – pankajdoharey Aug 31 '11 at 08:47
  • Because a class is also an object - an instance of the `Class` class. So what you are seeing is the instance methods of `Class`. If you want to see only the class methods of your own class, use `Person.methods(false)`. – Lars Haugseth Aug 31 '11 at 11:54
1
class Person
  def name
    puts "Doharey"
  end
end

How many instance methods are there in our new class?

Person.instance_methods.size
# => 72

List all instance methods of a class, excluding any methods inherited from the superclass:

Person.instance_methods(false)
# => [:name]

Every new class is by default a subclass of Object:

Person.superclass
# => Object

How many instance methods are there in the superclass?

Object.instance_methods.size
# => 71
Lars Haugseth
  • 14,721
  • 2
  • 45
  • 49