github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

shvets / design_patterns_in_ruby

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 165
    • 5
  • Source
  • Commits
  • Network (5)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Implementation of Design Patterns in Ruby — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

added act_as_observable to make dynamic code generation for observables Comment
shvets (author)
Sun Mar 22 08:28:29 -0700 2009
commit  698dcfab8a2d89909d49f8b6207beb7df4563bf1
tree    e63925a79d07d54eb8612e034e06f535a6768960
parent  b739593f4346a72e183c0b2ec95ea3ac5d6baace
design_patterns_in_ruby / creational / singleton.rb creational/singleton.rb
100644 38 lines (25 sloc) 0.89 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# singleton.rb
 
# Ensure a class only has one instance and provide a global point of access to it.
 
# Note: try to avoid singleton (see singleton-corrected.rb).
# Major question: how to test/mock the singleton?
 
class MySingleton1
  @@instance = nil
 
  def self.instance()
    if @@instance == nil
      public_class_method :new # enables instance creation for this class
 
      @@instance = new() unless @@instance
 
      private_class_method :new # disable instance creation for this class
    end
 
    @@instance
  end
 
  private_class_method :new # disable instance creation for this class
end
 
require 'singleton'
 
class MySingleton2
  include Singleton
end
 
# MySingleton1.new # error
 
puts "singleton1: " + MySingleton1.instance.to_s
puts "singleton1: " + MySingleton1.instance.to_s
 
puts "singleton2: " + MySingleton2.instance.to_s
puts "singleton2: " + MySingleton2.instance.to_s
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server