From: Hal Fulton Date: 2004-01-15T06:40:07+09:00 Subject: Nested scopes and the Singleton pattern Hello all, This is really two questions. I've been putting together a nested class structure in pieces since it is a little large and unwieldy. Here's a snippet of something I've run across. 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? Then I thought: Well, let's get rid of the unnecessary verbiage. (Remember, I'm REOPENING these classes.) 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. This does work: # Note: *Reopening* existing classes class ABC::XYZ @@instance = nil def initialize(foo) # Do some stuff... # and then: klass = ABC::XYZ def klass.new(foo) @@instance end end end Second question: Why should these two be different? Thanks, Hal Fulton