Skip to content

Instantly share code, notes, and snippets.

@yangxing-star
Created October 13, 2014 09:55
Show Gist options
  • Save yangxing-star/1dec53dac6906845c278 to your computer and use it in GitHub Desktop.
Save yangxing-star/1dec53dac6906845c278 to your computer and use it in GitHub Desktop.
class Logger
private_class_method :new
@@instance = nil
def info(msg)
puts msg
end
def self.instance
@@instance = new unless @@instance
end
end
logger = Logger.instance
logger.info('Hello')
@mimosa
Copy link

mimosa commented Oct 13, 2014

# -*- encoding: utf-8 -*-

class Test

  def initialize(*args, &blk)
    @instance ||= self.new(*args, &blk) # 保持一个实例.
  end

end

a = Test.new
b = Test.new

a == b
a == b

#=> ture

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment