If there's a block, pass in the file and close the file when it returns : block_given « Method « Ruby






If there's a block, pass in the file and close the file when it returns


  class File
    def File.my_open(*args)
      result = file = File.new(*args)
      
      if block_given?
        result = yield file
        file.close
      end

      return result
    end
  end

 








Related examples in the same category

1.Is block ready
2.The yield Statement using the block_given? method from Kernel.