[英]What is the difference between `Fixnum` and `3` in Ruby?
Fixnum.methods.count #=> 99
3.methods.count #=> 131
If they have different methods I would guess they are not exactly the same thing. 如果他们有不同的方法,我猜他们不是一回事。
What is the difference exactly? 到底有什么区别?
What's going on under the hood in Ruby? Ruby的幕后到底发生了什么?
I'm using ruby 2.2.1p85 (2015-02-26 revision 49769) . 我正在使用ruby 2.2.1p85(2015-02-26修订版49769) 。
As @Aetherus said, Fixnum
is an instance of a class, so Fixnum.methods
return all of its class methods, whereas 3.methods
will return Fixnum
instance methods, since 3
is an instance of Fixnum
. 正如@Aetherus所说, Fixnum
是一个类的实例,因此Fixnum.methods
返回其所有类方法,而3.methods
将返回Fixnum
实例方法,因为3
是Fixnum
的实例。
For example, basic math operators are instance methods, such as :+
, :-
, etc, so Fixnum.respond_to?(:+)
returns false
. 例如,基本的数学运算符是实例方法,例如:+
, :-
等,因此Fixnum.respond_to?(:+)
返回false
。 And 3.respond_to?(:class_variables)
returns false
, because that method is a class method, so instances of that class does not have access to it. 3.respond_to?(:class_variables)
返回false
,因为该方法是一个类方法,所以该类的实例无法访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.