Skip to content

Commit

Permalink
latest ruby design patterns stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
abramhindle committed Nov 20, 2012
0 parents commit fbd648e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Folders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class NamedSizedItem
@name = nil
@size = 0
def initialize(name)
@name = name
end
def name()
return @name
end
def size()
return @size
end
end

class NamedFile < NamedSizedItem
def initialize(name)
@name = name
@size = 1
end
end

class NamedFolder < NamedSizedItem
@files = []
def initialize(name)
@name = name
@files = []
end
def addFile(file)
@files << file
end
def size()
return @files.inject(0) { |res,elm| res + elm.size() }
end
end

file1 = NamedFile.new("readme")
file2 = NamedFile.new("license")
file3 = NamedFile.new("a.out")
subFolder1 = NamedFolder.new("Sub1");
subFolder2 = NamedFolder.new("Sub2");
subFolder3 = NamedFolder.new("Sub3");
subFolder1.addFile(file1)
subFolder2.addFile(file2)
subFolder3.addFile(file3)
subFolder2.addFile(subFolder1)
subFolder3.addFile(subFolder2)
puts(subFolder3.size())
8 changes: 8 additions & 0 deletions IncSingleton.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'singleton'
# http://dalibornasevic.com/posts/9-ruby-singleton-pattern-again

class IncSingleton
include Singleton
end


17 changes: 17 additions & 0 deletions MySingleton.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# http://apidock.com/ruby/Module/private_class_method
# http://dalibornasevic.com/posts/9-ruby-singleton-pattern-again
class MySingleton
# attempt to limit access to the constructor
private_class_method :new
# class variable instance
@@instance = nil
def self.instance()
if (@@instance.nil?())
# self. and MySingleton tend not to work
@@instance = self.new()
end
return @@instance
end
end


Binary file added ruby-design-patterns-partI.odp
Binary file not shown.

0 comments on commit fbd648e

Please sign in to comment.