Module: Kernel

Defined in:
lib/with_statement.rb

Instance Method Summary collapse

Instance Method Details

#with(*resources, &block) ⇒ Object

Similar to Python’s with statement. This statement enters the context of an object resource, executes the block, and exits the context.

Raises:

  • (SyntaxError)


6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/with_statement.rb', line 6

def with(*resources, &block)
  raise SyntaxError, 'with statement called with no arguments' if resources.size == 0
  if resources.size == 1
    simple_with(resources.shift, &block)
  else
    simple_with(resources.shift) do |what|
      with(*resources) {|*rs|
        args = [what] + rs
        block.call(*args)
      }
    end
  end
end