pankajdoharey
pankajdoharey

Reputation: 1572

Array initialization and method count increase ? What's the secret?

This Question may be weird or i am plain dumb.

>> Array.methods.count
=> 97
>> a = Array.new.methods
=> 167

What causes the increase in number of methods after an array has been initialized and assigned.

Upvotes: 1

Views: 157

Answers (1)

knut
knut

Reputation: 27875

You are counting two things: class-methods and instance methods. You may compare it with instance_methods

p Array.methods.count          #->  97
p Array.instance_methods.count #-> 167
p Array.new.methods.count      #-> 167

Or take a look if new is a valid method:

p Array.methods.include?(:new) #true
p Array.instance_methods.include?(:new) #false

new is only defined on the class, not in the instance.

Upvotes: 5

Related Questions