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
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:
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
On Fri, Dec 11, 2009 at 6:38 AM, Howard R.
[email protected] 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/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
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”
Enrique Comba R. wrote:
For more on Singletons you can refer to:
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
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