Sha256: 882faccaa0a2d19ce5e767102d020e44f7ab2dc13a8a2253b35e62693ee15722

Contents?: true

Size: 687 Bytes

Versions: 1

Compression:

Stored size: 687 Bytes

Contents

module Kernel

  # Similar to Python's with statement. This statement enters the
  # context of an object +resource+, executes the block, and exits the
  # context.
  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

  private

  def simple_with(resource, &block)
    what = resource.acquire
    begin
      yield(what)
    ensure
      resource.release
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
with_statement-0.2.0 lib/with_statement.rb