简体   繁体   中英

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 . Class doesn't define a * instance method (what would that even do), nor do Class 's ancestors ( Module , Object , Kernel , BasicObject ).

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.methods.sort
# => [:!, :!=, :!~, :%, :&, :*, :**, :+, :+@, :-, :-@, :/, :<, :<<, :<=, … ]

You can see that Fixnum defines an instance method named * :

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

因为它们不是Fixnum类方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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