def lock(file, mode)
      result = file.flock(mode)
      if result
        begin
          yield file if block_given?  # may not have block if called by locked?
        ensure
          # unlock but leave the file on the filesystem
          file.flock(File::LOCK_UN)
        end
      end
      return result
    end
def lock(file, mode)
      result = file.flock(mode)
      if result
        begin
          yield file if block_given?  # may not have block if called by locked?
        ensure
          # unlock but leave the file on the filesystem
          file.flock(File::LOCK_UN)
        end
      end
      return result
    end
Get a file lock with flock and ensure the
 lock is removed (but not the lockfile).  Accepts an optional
 code block.

 @example

     open('output', 'w') do |f|
       # exclusive blocking lock
       lock(f, File::LOCK_EX) do |f|
       f << "write to file while blocking other processes"
       end
     end

 @param [Object] file the file IO object returned from a call to "open(filename, mode_string)"
 @param [Constant] mode locking_constant, see File
 @return [Boolean] 0 or false if unable to sucessfully lock