Ruby - Wrap File object into you own class

Introduction

You could choose to assign the file handle to a class or instance variable:

Demo

class MyFile 
  attr_reader :handle #  www .j  ava  2 s. c  om

  def initialize(filename) 
    @handle = File.new(filename, "r") 
  end 

  def finished 
    @handle.close 
  end 
end 

f = MyFile.new("main.rb") 
puts f.handle.gets 
f.finished

Related Topic