Skip to content

Instantly share code, notes, and snippets.

@maeharin
Last active December 14, 2015 18:48
Show Gist options
  • Save maeharin/5131565 to your computer and use it in GitHub Desktop.
Save maeharin/5131565 to your computer and use it in GitHub Desktop.
composite pattern
class D
attr_accessor :name, :elements
def initialize; yield self; end
def print
puts "#{@name}/"
elements.each {|e| e.print}
end
end
class F
attr_accessor :name
def initialize; yield self; end
def print
puts @name
end
end
root_dir = D.new do |d|
d.name = 'htdocs'
d.elements = [
F.new {|f| f.name = '01.html'},
F.new {|f| f.name = '02.html'},
F.new {|f| f.name = '03.html'},
D.new do |d|
d.name = 'img'
d.elements = [
F.new {|f| f.name = 'a.img'},
F.new {|f| f.name = 'b.img'},
F.new {|f| f.name = 'c.img'},
]
end
]
end
root_dir.print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment