【发布时间】:2023-03-27 23:20:01
【问题描述】:
为什么singleton方法不能在Fixnum,Bignum,Float,Symbol类对象上定义,而FalseClass和TrueClass可以有?
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>'
【问题讨论】:
-
你的意思不是“实例对象”,而不是“类对象”吗?顺便说一句,
NilClass的实例对象也允许单例对象。