Module: Kernel

Defined in:
lib/with.rb

Overview

         DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                 Version 2, December 2004

Copyleft meh. [http://meh.paranoid.pk | [email protected]]

         DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.

++

Instance Method Summary collapse

Instance Method Details

#with(*objects, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/with.rb', line 14

def with (*objects, &block)
  result, exception = nil

  begin
    result = block.call *objects.map {|object|
      if object.respond_to? :__enter__
        object.__enter__
      else
        object
      end
    }
  rescue Exception => e
    exception = e
  end

  raise exception if objects.none? {|object|
    next unless object.respond_to?(:__exit__)

    if object.method(:__exit__).arity == 0
      object.__exit__ && false
    else
      object.__exit__(exception)
    end
  } && exception

  result
end