# Solve this ruby problem

**URL:** <https://www.ruby-forum.com/t/solve-this-ruby-problem/180094>\
**Category:** Ruby\
**Created:** [December 11, 2009, 10:21am UTC](https://www.ruby-forum.com/t/solve-this-ruby-problem/180094 "2009-12-11T10:21:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kgovinda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c68b51/32.png) [@kgovinda](https://www.ruby-forum.com/u/kgovinda)\
**Post date:** [December 11, 2009, 10:21am UTC](https://www.ruby-forum.com/t/solve-this-ruby-problem/180094/1 "2009-12-11T10:21:25Z")

</div>

below are the command and the error shows by complier:

class MyLogger  
private\_class\_method :new  
@@logger = nil  
def MyLogger.create  
@@logger = new unless @@logger  
@@logger  
end  
end  
MyLogger.create.id

the error is like this

1.rb:9: warning: Object#id will be deprecated; use Object#object\_id

thank you for you help  
and i want to know more about Singletons

---

<div class="post-metadata">

**Author:** ![kgovinda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c68b51/32.png) [@kgovinda](https://www.ruby-forum.com/u/kgovinda)\
**Post date:** [December 11, 2009, 10:45am UTC](https://www.ruby-forum.com/t/solve-this-ruby-problem/180094/2 "2009-12-11T10:45:49Z")

</div>

Hello Govinda,

your sample program does not have any error as such.

The message you are getting is a warning that Object#id is going to be  
deprecated in future Ruby versions and you should consider using  
Object#object\_id instead so that your program will run in latter  
versions.

For more on Singletons you can refer to:

- [Singleton pattern - Wikipedia](http://en.wikipedia.org/wiki/Singleton_pattern)

Basically in Ruby implementing a singleton is as simple as (inside of  
IRB):

> > require ‘singleton’  
> > =\> true  
> > class MyLogger  
> > include Singleton  
> > end  
> > =\> MyLogger  
> > MyLogger.new  
> > NoMethodError: private method `new’ called for MyLogger:Class  
> > from (irb):5  
> > MyLogger.instance  
> > =\> #MyLogger:0x1011b5808

Hope this helps.

Enrique

---

<div class="post-metadata">

**Author:** ![kgovinda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c68b51/32.png) [@kgovinda](https://www.ruby-forum.com/u/kgovinda)\
**Post date:** [December 11, 2009, 2:50pm UTC](https://www.ruby-forum.com/t/solve-this-ruby-problem/180094/3 "2009-12-11T14:50:28Z")

</div>

On Fri, Dec 11, 2009 at 6:38 AM, Howard R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I can tell you this:
> 
> (pretty much) Everything in Ruby is a class

No my friend, most things in Ruby are not classes, (pretty much)  
everything in Ruby is an object, and objects have classes, some  
objects have singleton classes, which give them extra methods or  
override methods they would have otherwise gotten from their class and  
its parents (superclasses). Classes, themselves, being objects have a  
class called Class, and all have a singleton class which gives them  
additional methods (class methods) which are in addition to or  
override methods provided by Class.

There’s at least one subtle difference, relating to the meaning of  
singleton, between singleton classes of ordinary objects and singleton  
classes of classes. Methods defined in the singleton class of an  
ordinary object only apply to that individual object instance, whereas  
methods defined in the singleton class of a class are available to  
that class and any of its subclasses. The difference is that in the  
first case the superclass field of the singleton class refers to the  
object’s class, while in the latter it refers to the singleton class  
of the class’s superclass. So while some consider class methods to be  
‘singleton methods’ of the class, a case can be made that this isn’t  
really true, particularly if the class has any subclasses, because the  
class methods apply to more than a single object.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![kgovinda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c68b51/32.png) [@kgovinda](https://www.ruby-forum.com/u/kgovinda)\
**Post date:** [December 11, 2009, 3:05pm UTC](https://www.ruby-forum.com/t/solve-this-ruby-problem/180094/4 "2009-12-11T15:05:12Z")

</div>

Rick Denatale wrote:

> No my friend, most things in Ruby are not classes, (pretty much)  
> everything in Ruby is an object

Thanks for catching this very bad slip…I’ll blame this (and the rest  
of my bad grammar in my original reply) on caffeine deficiency.

> So while some consider class methods to be  
> ‘singleton methods’ of the class, a case can be made that this isn’t  
> really true, particularly if the class has any subclasses, because the  
> class methods apply to more than a single object.

Thanks again-- good stuff to take in while one tries absorb and digest  
all of this. And I’m glad someone as “well versed” as yourself “chimed  
in” 😉

---

<div class="post-metadata">

**Author:** ![kgovinda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c68b51/32.png) [@kgovinda](https://www.ruby-forum.com/u/kgovinda)\
**Post date:** [December 11, 2009, 12:38pm UTC](https://www.ruby-forum.com/t/solve-this-ruby-problem/180094/5 "2009-12-11T12:38:03Z")

</div>

Enrique Comba R. wrote:

> For more on Singletons you can refer to:
> 
> - [Singleton pattern - Wikipedia](http://en.wikipedia.org/wiki/Singleton_pattern)

Please don’t get the above information about the ‘Singleton Pattern’  
confuse your efforts to understand Ruby’s very special use of the term  
Singleton Class, which is completely different. Jim W. does a good  
job of explaining this confusion here:

[http://onestepback.org/index.cgi/Tech/Ruby/Metaclasses.red](http://onestepback.org/index.cgi/Tech/Ruby/Metaclasses.red)

Hopefully someone more well versed in the subject than me will chime in  
soon, but as this is a trap that got me for, I can tell you this:

(pretty much) Everything in Ruby is a class, and every class (including  
your MyLogger class) has a hidden, anonymous class where methods are  
stored. This is called the Singleton class (or eigenclass, or  
metaclass-- don’t be too worried about the name- it’s been point of  
discussion for lots of people).

One reason this Singleton class exists is to be able to place methods  
like your MyLogger.create into the object’s (everything in Ruby is an  
object) lookup chain somewhere, and to give you the ability to change  
what the context and scope of variables (and what ‘self’ refers to) that  
you use.

There are several ways to create a singleton class-- your def  
MyLogger.create method is perhaps the most basic.

Once someone gets further along in learning Ruby they might need to use  
methods like class\_eval, method\_missing, instance\_eval, creating methods  
dynamically, and need to start mixing in methods from modules into  
objects dynamically. Once this point is reach a good grasp of Ruby’s  
concept of the Singleton Class is required. In the meantime, a beginner  
needs to know that every object does have a Singleton Class, and that  
‘Metaprogramming’ is just plain old Ruby that manipulates the Singleton  
Class in very useful ways.

HTH,  
Howard
