Add class method to File class : File class « File Directory « Ruby






Add class method to File class


  class File
    def File.open_and_process(*args)
      f = File.open(*args)
      yield f
      f.close()
    end
  end

  File.open_and_process("testfile", "r") do |file|
    while line = file.gets
      puts line
    end
  end

 








Related examples in the same category

1.Override file open method