PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Singleton object vs. enhancing singleton class
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Singleton object vs. enhancing singleton class

Réponse
 
LinkBack Outils de la discussion
Vieux 09/06/2008, 04h35   #1
Paul McMahon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Singleton object vs. enhancing singleton class

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

  Réponse avec citation
Vieux 09/06/2008, 05h02   #2
David Masover
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Singleton object vs. enhancing singleton class

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.

  Réponse avec citation
Vieux 09/06/2008, 05h04   #3
David Masover
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Singleton object vs. enhancing singleton class

In reply to myself:

> it prevents the class from being instantiated


To clarify -- prevents it from being instantiated again.

  Réponse avec citation
Vieux 09/06/2008, 07h05   #4
David A. Black
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Singleton object vs. enhancing singleton class

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!

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h24.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09706 seconds with 12 queries