Last active
November 17, 2015 00:52
-
-
Save eoinkelly/5293a7fda59747ec6ccb to your computer and use it in GitHub Desktop.
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
## | |
# | |
class Connection | |
def self.create_many(inits) | |
inits.map { |init| new(init) } | |
end | |
def self.for_all(conns, &block) | |
block.call(conns) | |
end | |
def self.open_all(conns) | |
conns.each(&:open) | |
end | |
def self.close_all(conns) | |
conns.each(&:close) | |
end | |
attr_reader :args | |
def initialize(args) | |
puts "init #{args}" | |
@args = args | |
end | |
def open | |
puts "open #{@args}" | |
end | |
def close | |
puts "close #{@args}" | |
end | |
end | |
c1_args = :c1 | |
c2_args = :c2 | |
c3_args = :c3 | |
inits = [c1_args, c2_args, c3_args] | |
conns = Connection.create_many(inits) | |
Connection.open_all(conns) | |
Connection.for_all(conns) do |c1, c2, c3| | |
puts "doing stuff with #{c1.args}" | |
puts "doing stuff with #{c2.args}" | |
puts "doing stuff with #{c3.args}" | |
end | |
Connection.close_all(conns) | |
# output is ... | |
# | |
# init c1 | |
# init c2 | |
# init c3 | |
# open c1 | |
# open c2 | |
# open c3 | |
# doing stuff with c1 | |
# doing stuff with c2 | |
# doing stuff with c3 | |
# close c1 | |
# close c2 | |
# close c3 | |
# output should be... | |
# open c1 | |
# init c1 | |
# open c2 | |
# init c2 | |
# open c3 | |
# init c3 | |
# doing stuff with c1 | |
# doing stuff with c2 | |
# doing stuff with c3 | |
# close c3 | |
# close c2 | |
# close c1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment