Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the `singleton` methods can't be defined on `Fixnum`,`Bignum`,`Float`,`Symbol` class objects, but ` FalseClass` and `TrueClass` can have?

Tags:

ruby

ruby-2.0

Why the singleton methods can't be defined on Fixnum,Bignum,Float,Symbol class objects, but FalseClass and TrueClass can have?

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>'
like image 545
Arup Rakshit Avatar asked Jan 14 '23 22:01

Arup Rakshit


2 Answers

As the Ruby Docs say:

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.

The same is true for Bignum, Float and Symbol

like image 150
sergelerator Avatar answered Jan 21 '23 21:01

sergelerator


From the 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.

That would apply to other primitive numeric types and Symbol.

like image 41
David Unric Avatar answered Jan 21 '23 19:01

David Unric