Created
October 6, 2011 20:35
-
-
Save mcrmfc/1268586 to your computer and use it in GitHub Desktop.
ruby singleton examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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