When did IO::read appear?

I wrote some code on a ruby 1.7.2 (2002-05-29) machine using
IO.read, but when I moved it to a ruby 1.6.8 (2002-12-24)
machine I got an exception,

NameError: undefined method `read’ for IO:Class

What’s the idiomatic workaround for this sort of thing?

Thanks,

···

–
Bil Kleb, NASA, Hampton, Virginia, USA

To be backwards compatible, you can use:

 content = File.open(name) { |f| f.read}

Cheers

Dave

···

On Jan 4, 2004, at 9:41, Bil Kleb wrote:

I wrote some code on a ruby 1.7.2 (2002-05-29) machine using
IO.read, but when I moved it to a ruby 1.6.8 (2002-12-24)
machine I got an exception,

NameError: undefined method `read’ for IO:Class

What’s the idiomatic workaround for this sort of thing?

What's the idiomatic workaround for this sort of thing?

redefine it, something like (not tested)

   unless IO::respond_to?(:read)
      def IO::read(file, lgth = nil, offset = 0)
         Kernel::open(file) do |io|
            io.seek(offset) if !offset.zero?
            io.read(lgth)
         end
      end
   end

Guy Decoux