When did IO::read appear?

B

Bil Kleb

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,
 
D

Dave Thomas

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?

To be backwards compatible, you can use:

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


Cheers

Dave
 
T

ts

B> 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::eek:pen(file) do |io|
io.seek(offset) if !offset.zero?
io.read(lgth)
end
end
end


Guy Decoux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top