Skip to content

Instantly share code, notes, and snippets.

@steveh
Last active November 16, 2015 23:13
Show Gist options
  • Save steveh/b1ab77dc297d53350641 to your computer and use it in GitHub Desktop.
Save steveh/b1ab77dc297d53350641 to your computer and use it in GitHub Desktop.
class Connection
def self.combine(*args, &block)
# call open multiple times somehow?
end
def self.open(args = {}, &block)
puts "open #{args}"
instance = new(args)
block.call(instance)
puts "close #{args}"
end
attr_reader :args
def initialize(args)
puts "init #{args}"
@args = args
end
end
c1_args = :c1
c2_args = :c2
c3_args = :c3
# this works...
# Connection.open(c1_args) do |c1|
# Connection.open(c2_args) do |c2|
# Connection.open(c3_args) do |c3|
# puts "doing stuff with #{c1.args}"
# puts "doing stuff with #{c2.args}"
# puts "doing stuff with #{c3.args}"
# end
# end
# end
# but want this API...
Connection.combine(c1_args, c2_args, c3_args) do |c1, c2, c3|
puts "doing stuff with #{c1.args}"
puts "doing stuff with #{c2.args}"
puts "doing stuff with #{c3.args}"
end
# 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