0

Why does the total count of methods reduce, from 81 to 46 while instantiating an object from 'Class' class-objects?

Here's the code I'm running:

class Automobile
    def wheels(wheel)
        puts "#{wheel}"
    end
end


class Car < Automobile
    def gears
        puts "Automatic Transmission"
    end
end


limo = Car.new
benz = Automobile.new

puts Automobile.methods.count
puts Car.methods.count

puts benz.methods.count
puts limo.methods.count

I guess subclass is not inheriting certain methods, I thought they are class methods, so I did some tests and realized methods displayed by "puts Anyclass.methods" are not class methods. They must be instance methods.

How is this achieved in Ruby, to deter a subclass from inheriting certain methods?

random
  • 9,774
  • 10
  • 66
  • 83
pankajdoharey
  • 1,562
  • 19
  • 30

1 Answers1

2

Your entire question seems to be based on the incorrect belief that the result of Car.methods is not the class methods of the Car class, but its instance methods. The result of Car.methods is the list of methods of the Car class itself. To get the instance methods, you would have to write Car.instance_methods. That's why you see that the instances have fewer methods than the classes.

For me, here are the results of running your code:

puts Automobile.methods.count 
  #=> 95
puts Car.methods.count 
  #=> 95 (exactly as you'd expect, since it didn't define any new class methods)
puts benz.methods.count
  #=> 57 (this is 1 more than the result of Object.instance_methods.count, since you added #wheels)
puts limo.methods.count
  #=> 58 (1 more than benz.methods.count, since you added #gears)
Chuck
  • 234,037
  • 30
  • 302
  • 389
  • 1) Car.methods is the list of methods in Car class and it is based on 'Class' class and not 'String' class. – pankajdoharey Aug 30 '11 at 20:30
  • @pankajdoharey: Yes, that's what I said. If my answer says "String" anywhere, I can't find it, but it should be "Car" at any rate. It said "String" when I very first posted it, but I corrected it in about 30 seconds, so I don't know how you'd be seeing that 15 minutes later. – Chuck Aug 30 '11 at 20:37
  • 2) isn't limo based on Car class itself so it should inherit all the methods from Car and count should be equal right? – pankajdoharey Aug 30 '11 at 20:38
  • @pankajdoharey: It is an instance of the Car class, so it gets all of Car's instance methods. It does not get Car's class methods. – Chuck Aug 30 '11 at 20:40
  • ok just last doubt, what you are saying is that, when i say Car.methods the list of methods produced are class methods right ? and if they are class methods then they should not be inherited right ? but we see methods like new,methods and methods_defined? why is that so ? – pankajdoharey Aug 30 '11 at 20:45
  • @pankajdoharey: Class methods are inherited by subclasses. – Chuck Aug 30 '11 at 20:45
  • Yes they are but not by instance of a class. Class methods by definition are not present in instance of a class. – pankajdoharey Aug 30 '11 at 20:50
  • @pankajdoharey: Yes, that's true. I'm not following where you're confused. – Chuck Aug 30 '11 at 20:52
  • man i must suck at asking questions, anyways one more try please bare with me. so What does Car.methods produce, a list of class methods in Car class ...? – pankajdoharey Aug 30 '11 at 21:07
  • @Chuck let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3012/discussion-between-pankajdoharey-and-chuck) – pankajdoharey Aug 30 '11 at 21:27
  • @pankajdoharey: Yes, that's right. Calling `any_object.methods` gives you a list of the methods that `any_object` can perform. For a class, these are its class methods. For an instance, these are the instance methods of its class plus any methods that have been added to that specific object. – Chuck Aug 30 '11 at 21:27
  • i have really no clue how to ask my doubt. – pankajdoharey Aug 30 '11 at 21:50
  • Please come over to this question. http://stackoverflow.com/questions/7250523/is-class-declaration-an-eyewash-in-ruby-is-everything-really-object-oriented this time it may be better framed. – pankajdoharey Aug 30 '11 at 22:26