I was playing with singleton class in my IRB. And doing so tried the below snippets.
class Foo ; end #=> nil foo = Foo.new #=> #<Foo:0x2022738> foo.define_singleton_method(:bar , method(:puts)) #=> #<Method: Object(Kernel)#puts> Here above I just created a singleton method on instance of class Foo.
foo.bar("hi") hi #=> nil foo.singleton_methods #=> [:bar] foo_sing = foo.singleton_class #=> #<Class:#<Foo:0x2022738 foo_sing.is_a? Class #=> true foo_sing.instance_of? Class #=> true foo_sing.inspect #=> "#<Class:#<Foo:0x1e06dc8>>" In the above I tried to create a singleton class on instance of class Foo. And also tested if foo_sing holds reference to the singleton class on instance of class Foo.
foo_sing.methods(false) #=> [] foo_sing.methods.include? :bar #=> false In the above I was looking if singleton_method bar is in the foo_sing or not.But found it is not present there.Then my question is - where does thosesingleton_method reside in Ruby?
foo_sing.new.methods(false) #TypeError: can't create instance of singleton class # from (irb):10:in `new' # from (irb):10 # from C:/Ruby193/bin/irb:12:in `<main>' class Bas < foo_sing ; end #TypeError: can't make subclass of singleton class # from (irb):16 # from C:/Ruby193/bin/irb:12:in `<main>' In the above part I was checking if we can create,instance of singleton class or not and subclass of singleton class,like regular class. But the answers I found as NO. What is the concept or theory or purpose behind it ?
Again in the following code I can see the that same name methods are being overridden inside a singleton class. But when I am searching that method inside the class not found as I asked above.
class Foo ; end #=> nil foo = Foo.new #=> #<Foo:0x225e3a0> def foo.show ; puts "hi" ; end #=> nil foo.show #hi #=> nil class << foo ;def show ; puts "hello" ; end ;end #=> nil foo.show #hello #=> nil