Skip to content

Instantly share code, notes, and snippets.

@mcrmfc
Created October 6, 2011 20:35
Show Gist options
  • Save mcrmfc/1268586 to your computer and use it in GitHub Desktop.
Save mcrmfc/1268586 to your computer and use it in GitHub Desktop.
ruby singleton examples
require 'singleton'
class MySingleton
include Singleton
attr_accessor :myvar
def initialize
puts 'output from initialize in a singleton!'
end
end
MySingleton.instance.myvar = 'foo'
puts MySingleton.instance.myvar
class MyStaticClass
@myvar = 'bar'
class << self
attr_accessor :myvar
end
end
puts MyStaticClass.myvar
class MyUglyStaticClass
@@myvar = 'cat'
def self.cat
@@myvar
end
def self.set_cat(name)
@@myvar =cat
end
end
puts MyUglyStaticClass.cat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment