0.0
No commit activity in last 3 years
No release in over 3 years
With statements opens the Kernel and defines a with method which behaves similar to Python with statement
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0.0
~> 1.5.1
>= 0
 Project Readme

with_statement¶ ↑

This simple gem implements a Kernel#with method much like Python’s with statement.

Simple usage:

with expression do |resource|
  # ... interact with resource ...
end

The expression must return a context manager. A context manager is an object which implements two methods: acquire and release. When Kernel#with receives the context manager it calls its acquire method and grab the result. Then it yields the result to the block. When the block returns, the context manager send the release message to the context manager.

Kernel#with by itself does not attempt to run acquire or release atomically; the context manager should ensure that if required.

Since 0.2.0 release you can put several context managers in a single with sentence:

with exp1, exp2 do |r1, r2|
  # do something with the acquired resources
end

That sentence it’s semantically equivalent to:

with exp1 do |r1|
  with exp2 do |r2|
    # do something with the acquired resources
  end
end

The general syntax for the Kernel#with method is:

with(*managers){|*resources|
  #code
}

If any of the context managers fails to enter (acquire), the block is never executed and any previous acquired resource is released.

Non context managers¶ ↑

As of version 0.3.0, with allows non-context managers to be passed as arguments. This is make with an idiomatic substitution for Object#tap.

with 12 + 98 do |result|
  puts result
end

Contributing to with_statement¶ ↑

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2010, 2011 Manuel Vázquez Acosta. See LICENSE.txt for further details.