Permalink
Browse files

More singleton examples

  • Loading branch information...
1 parent 326a45c commit 6fcc8a238b8082cd0ff0698f98910e5fa7fdd42b @sandro sandro committed Jul 28, 2009
Showing with 44 additions and 0 deletions.
  1. +44 −0 12/three_module_singletons.rb
@@ -0,0 +1,44 @@
+module Site
+ # Thanks to Chris Wanstrath http://ozmm.org/posts/singin_singletons.html
+ extend self
+ attr_accessor :url
+
+ def ping
+ puts "pinging #{url}"
+ end
+end
+
+Site.url = "http://google.com"
+Site.ping
+Object.send :remove_const, :Site
+
+
+
+module Site
+ class << self
+ attr_accessor :url
+
+ def ping
+ puts "pinging #{url}"
+ end
+ end
+end
+
+Site.url = "http://google.com"
+Site.ping
+Object.send :remove_const, :Site
+
+
+
+module Site
+ class << self; attr_accessor :url end
+ module_function
+
+ def ping
+ puts "pinging #{url}"
+ end
+end
+
+Site.url = "http://google.com"
+Site.ping
+Object.send :remove_const, :Site

0 comments on commit 6fcc8a2

Please sign in to comment.