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 # from  www.  j  a  v  a  2s  . com

  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