From: nobu.nokada@... Date: 2004-01-15T10:49:25+09:00 Subject: Re: Nested scopes and the Singleton pattern Hi, At Thu, 15 Jan 2004 06:40:07 +0900, Hal Fulton wrote: > By the way, my last change was to implement a simple version of the > Singleton pattern (without using singleton.rb). > > > # Note: *Reopening* existing classes > > class ABC > class XYZ > @@instance = nil > def initialize(foo) > # Do some stuff... > # and then: > def XYZ.new(foo) > @@instance > end > end > end > end > > > This works fine. > > First question: Is this a valid way to implement Singleton, or am I > overlooking something? Have you considered about race conditions? > So I did: > > # Note: *Reopening* existing classes > > class ABC::XYZ > @@instance = nil > def initialize(foo) > # Do some stuff... > # and then: > def ABC::XYZ.new(foo) > @@instance > end > end > end > > But it didn't work. def ABC::XYZ means singleton method XYZ of ABC by itself, so trailing . is superfluous. Try def (ABC::XYZ).new(foo) -- Nobu Nakada