3

I understand that singleton methods live in singleton classes (or eigenclasses). My first question is:

Why can't they live inside objects? I know that in ruby, an object can only hold instance variables. But I cannot see why singleton methods weren't designed to be in objects.

Next,

If objects can only contain instance variables, how come classes, which are objects of Class, are able to store methods?

I am just confused at conceptual level. I have read many times that ruby object model is consistent.

4 Answers 4

2

Ad 1, they indeed do live inside an object. Singleton class, in fact, is just a mental construct. 'Singleton' here means, that, by definition, the class has only one instance - that object. So the identity of the object and the singleton class is intextricably the same.

Ad 2, the answer is, because Matz decided so. More precisely, not classes, but modules (Module class) store methods, and Class < Module.

It might be of interest to you, that a singleton class of an object is not instantiated by default. Until you try to access it, it simply does not exist. This is because if all the singleton classes were immediately instantiated, you would have an infinite chain of singleton classes:

o = Object.new
o.singleton_class
o.singleton_class.singleton_class
o.singleton_class.singleton_class.singleton_class
...
Sign up to request clarification or add additional context in comments.

Comments

2

The goal of singleton pattern is to ensure, that only one object of a particular class will live within a system.

So the methods still live in a class, but you just can't create another instance of that class.

A class is an object, but because it serves as a template for other objects, its own instances, it keeps their instance methods inside its module. But if you treat a class as an ordinary object, for example call #object_id on it, it gives you all the behavior expected of normal objects.

11 Comments

Thanks for response. I know how they work. I am confused about their design. Wouldn't it have been easier if objects can hold methods?
I think that "singleton pattern" in this form went out of fashion at least 5 years ago (in Ruby community, at least).
@megas: Are you smoking something?
@megas: Oh, I got it, ты Русский :-))) Work on your English some more :-)
I think I should settle for the fact that classes are slightly special objects which can indeed store methods. Otherwise they won't be able to do what they do. I liked the consistency in object model and suddenly these special cases start arising.
|
1

Continuing from our discussion in comments: Strictly speaking, the ability to store methods (and constants) is a buit-in special ability of modules (Module class). Class class happens to be a subclass of Module class, which makes it inherit modules' ability to store methods, but you are free to create other objects (not just classes) with that ability:

class MyPetObjectThatCanStoreMethods < Module
  def greet; puts "Bow, wow!" end
end

Fred = MyPetObjectThatCanStoreMethods.new

So, Fred is not exactly a module, but decsends from Module and has thus ability to store methods and constants:

module Fred # this is allowed!
  LENGTH = 19
  def rise; puts "I'm rising!" end
end

You can't do this with normal objects, only with module descendants!

Fred::LENGTH #=> 19
Fred.greet
#=> Bow, wow!
Fred.rise
#=> NoMethodError

That's because Fred stores #rise method, just like modules do, but unlike #greet method, #rise is not its instance method! To gain access to it, we will have to include Fred in a suitable class:

o = Object.new
o.rise
#=> NoMethodError
class << o  # Including Fred in the metaclass of o
  include Fred
end
o.rise
#=> I'm rising!

2 Comments

add your answer to your existing answer,why using separate one?
I never did that until today, and come today, bang, I did that twice. I'm doing it because SO allows and enables it. It's just like with Ruby coding: Don't fear to use the features that exist, be it eval, for loops, parsing html with regexen... You are the programmer, you decide. So I decided to make a second separate answer today, already twice.
1

If objects can only contain instance variables, how come classes, which are objects of Class, are able to store methods?

class Foo
  def suprim;end
end
Foo.instance_methods(true).grep(/^sup/)
#=> [:suprim]

I searched for Class's instance method superclass ,but didn't appear,as Foo is an instance of Class,so it should not store the instance methods of Class. But yes,Foo returns only the methods of its instances to be used. Which is natural.Look below now:

Class.instance_methods(true).grep(/^sup/)
#=>[:superclass]

3 Comments

With that grep /^s/, what is the exact question you are exploring?
i took an example of superclass.
I see, so this is sort of a demonstration that instance methods of a class are not instance methods of the classe's instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.