O'Reilly logo
  • Sarma Palli thinks this is interesting:

def self.acquire
    lock = new  # Initialize the resource
    lock.exclusive_lock!
    yield(lock) # Give it to the block
  ensure
    # Make sure it gets unlocked.
    lock.unlock i...

From

Cover of Effective Ruby: 48 Specific Ways to Write Better Ruby

Note

def self.acquire lock = new # Initialize the resource lock.exclusive_lock! return lock unless block_given? begin yield(lock) # Give it to the block ensure # Make sure it gets unlocked. lock.unlock if lock end end