Hatena::Groupruby

kdmsnrの日記

1000-01-12

Singleton

18:31

class Single
  @@instance = Single.new
  def self.instance
    return @@instance
  end
end

p Single.instance #<Single:0x2bb67bc> 同じ
p Single.instance #<Single:0x2bb67bc> 同じ
p Single.new #<Single:0x2bb5d44> 違う

#-------------------------------------------

require 'singleton'
class Single
  include Singleton
end

p Single.instance #<Single:0x2ba7294> 同じ
p Single.instance #<Single:0x2ba7294> 同じ
p Single.new # private method `new' called for Single:Class (NoMethodError)