简体   繁体   English

Ruby:列出Fixnum方法不包括算术运算符

[英]Ruby: listing Fixnum methods does not include arithmetic operators

Fixnum.methods.sort
=> [:!, :!=, :!~, :<, :<=, :<=>, ..., :trust, :untaint, :untrust, :untrusted?]

Why doesn't it display :* , :/ , :+ , :- , :% (or :"*" , ":/" , etc.) as methods? 为什么它不显示:*:/:+:-:% (或:"*"":/"等)作为方法?

I see that they are considered methods. 我看到它们被认为是方法。

Fixnum is an instance of Class . FixnumClass的实例。 Class doesn't define a * instance method (what would that even do), nor do Class 's ancestors ( Module , Object , Kernel , BasicObject ). Class没有定义*实例方法(你会连做),也没有Class的祖先( ModuleObjectKernelBasicObject )。

Now, 1 on the other hand is an instance of Fixnum , and since Fixnum defines a * instance method, that instance method shows up when you ask 1 about its methods: 现在, 1 ,另一方面是实例Fixnum ,而且由于Fixnum定义*实例方法,该实例方法显示,当你问了1关于它的方法:

1.methods.sort
# => [:!, :!=, :!~, :%, :&, :*, :**, :+, :+@, :-, :-@, :/, :<, :<<, :<=, … ]

You can see that Fixnum defines an instance method named * : 您可以看到Fixnum定义了一个名为*的实例方法:

Fixnum.instance_methods.sort
# => [:!, :!=, :!~, :%, :&, :*, :**, :+, :+@, :-, :-@, :/, :<, :<<, :<=, … ]

因为它们不是Fixnum类方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM