开发者

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

开发者 https://www.devze.com 2023-04-08 14:42 出处:网络
This Question may be weird or i am plain dumb. >> Array.methods.count => 97 >> a = Array.new.methods

This Question may be weird or i am plain dumb.

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

W开发者_如何学Pythonhat causes the increase in number of methods after an array has been initialized and assigned.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消