gpt4 book ai didi

ruby - 为什么不能在 `singleton` `Fixnum` ,`Bignum` ,`Float` 类对象上定义 ,`Symbol` 方法,但 ` FalseClass` 和 `TrueClass` 可以有?

转载 作者:数据小太阳 更新时间:2023-10-29 07:50:23 27 4
gpt4 key购买 nike

为什么不能在Fixnum,Bignum,Float,上定义singleton方法Symbol类对象,但是FalseClassTrueClass可以有吗?

C:\>ruby -v
ruby 2.0.0p0 (2013-02-24) [i386-mingw32]

C:\>irb --simple-prompt
DL is deprecated, please use Fiddle
11111111111.class
#=> Bignum
class << 11111111111 ; end
#TypeError: can't define singleton
# from (irb):2
# from C:/Ruby200/bin/irb:12:in `<main>'

1111.class
#=> Fixnum
class << 1111 ; end
#TypeError: can't define singleton
# from (irb):4
# from C:/Ruby200/bin/irb:12:in `<main>'

11.11.class
#=> Float
class << 11.11 ; end
#TypeError: can't define singleton
# from (irb):6
# from C:/Ruby200/bin/irb:12:in `<main>'

:name.class
#=> Symbol
class << :name ; end
#TypeError: can't define singleton
# from (irb):8
# from C:/Ruby200/bin/irb:12:in `<main>'

最佳答案

作为Ruby Docs说:

There is effectively only one Fixnum object instance for any given integer value, so, for example, you cannot add a singleton method to a Fixnum.

BignumFloatSymbol 也是如此

关于ruby - 为什么不能在 `singleton` `Fixnum` ,`Bignum` ,`Float` 类对象上定义 ,`Symbol` 方法,但 ` FalseClass` 和 `TrueClass` 可以有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15418576/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com