2

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 
5
  • Why is it important where they live? Isn't it a lot more important that they work? If it's really important to you, look in the source code. Commented Mar 16, 2013 at 16:32
  • I think @iAmRubuuu wants to gain understanding about singleton classes. Commented Mar 16, 2013 at 16:34
  • BTW, I will post a blog post in a few days and will try to answer these. Commented Mar 16, 2013 at 16:35
  • @Marc-AndréLafortune yes, you are right. Thanks to understand my curiosity. Commented Mar 16, 2013 at 16:36
  • @Marc-AndréLafortune Woww, Nice to hear from you that. :) Commented Mar 16, 2013 at 16:37

1 Answer 1

2

You're on the right track.

1) When looking for the method in the singleton class, you wanted to use instance_methods, not methods:

foo_sing.instance_methods.include? :bar # => true # or foo_sing.method_defined? :bar # => true 

It is a bit confusing, since method_defined? really means "instance methods defined?", while methods really means singleton methods...

2) You can't subclass or instantiate a singleton class because it is meant to be a singleton, i.e. have exactly one instance.

It doesn't matter anyways, since you should instead use mixins for code that you want to reuse. These can be included/prepended in as many singleton classes or normal classes as you want:

foo.extend ResuableFunctionality # or equivalently: class << foo include ReusableFunctionality end 
Sign up to request clarification or add additional context in comments.

1 Comment

Sure I will. I wanna to see all the answers or more on that. If you write it on your post,it would be great. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.