Skip to content

Instantly share code, notes, and snippets.

@eoinkelly
Last active November 17, 2015 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eoinkelly/5293a7fda59747ec6ccb to your computer and use it in GitHub Desktop.
Save eoinkelly/5293a7fda59747ec6ccb to your computer and use it in GitHub Desktop.
##
#
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