Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Singleton object vs. enhancing singleton class

Reply
Thread Tools

Singleton object vs. enhancing singleton class

 
 
Paul McMahon
Guest
Posts: n/a
 
      06-09-2008
In ruby, as class are singleton objects, does anyone have any ideas when
a a singleton object should be used vs. simply adding methods to the
class? For example,

class Foo
include Singleton
def bar
end
end

vs.

class Foo
def self.bar
end
end

 
Reply With Quote
 
 
 
 
David Masover
Guest
Posts: n/a
 
      06-09-2008
On Sunday 08 June 2008 22:35:25 Paul McMahon wrote:
> In ruby, as class are singleton objects, does anyone have any ideas when
> a a singleton object should be used vs. simply adding methods to the
> class?


I always include Singleton, but I see what you mean. A better way might be to
do it with modules:

module Foo
def self.bar
end
end

One example is mixins, though. For example, here:

> class Foo
> def self.bar
> end
> end


A mixin would almost certainly be expecting to be mixed via "include" here,
not "extend". This might not always matter, but some mixins will define
things like self.included, and won't work at all if you try to extend them
instead.

However, a mixin which expected "extend" would still work on a Singleton
object:

class Foo
include Singleton
def bar
end
end
Foo.instance.extend SomeMixin

I think the main reason for doing Singleton, though, is that it expresses
intent better, and it prevents the class from being instantiated -- sure,
there are ways around it, but you're not going to accidentally call Foo.new
without getting an error.

 
Reply With Quote
 
 
 
 
David Masover
Guest
Posts: n/a
 
      06-09-2008
In reply to myself:

> it prevents the class from being instantiated


To clarify -- prevents it from being instantiated again.

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      06-09-2008
Hi --

On Mon, 9 Jun 2008, Paul McMahon wrote:

> In ruby, as class are singleton objects, does anyone have any ideas when


I'd just say: classes are objects. Like other objects, they can have
methods added to them on a per-object basis (give or take the fact
that "per-object" in the case of a class actually includes
subclasses).

> a a singleton object should be used vs. simply adding methods to the
> class? For example,
>
> class Foo
> include Singleton
> def bar
> end
> end
>
> vs.
>
> class Foo
> def self.bar
> end
> end


I don't see these techniques as addressing the same problem or being
likely candidates to replace each other. I'd go by what you need. If
you need a class that has only one instance, then use Singleton. If
you've got a situation where there's class-level (as opposed to
instance-level) knowledge involved, then use a class method.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
ADVANCING WITH RAILS July 21-24 Edison, NJ
See http://www.rubypal.com for details and updates!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Singleton methods without the singleton class Charles Oliver Nutter Ruby 4 03-22-2010 10:46 PM
Singleton Modules rather than Singleton Classes Trans Ruby 12 09-14-2007 06:45 AM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
Singleton classes and Singleton pattern Wilhelm Ruby 1 10-11-2006 01:08 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM



Advertisments