IO::write

  • Thread starter Charles Comstock
  • Start date
C

Charles Comstock

Why is there only an IO::read, and no equivalent IO::write that takes a
filename, a string to write, and an optional offset in the file to start
writing at? Or better yet an IO::write(filename[,offset])= string to
write to file. I don't know it seems useful to me for quickly writing a
response out to a file anyone else?

Charles Comstock
 
H

Hal Fulton

Charles said:
Why is there only an IO::read, and no equivalent IO::write that takes a
filename, a string to write, and an optional offset in the file to start
writing at? Or better yet an IO::write(filename[,offset])= string to
write to file. I don't know it seems useful to me for quickly writing a
response out to a file anyone else?

This seems reasonable to me.

We *might* also want a writelines that corresponded to readlines --
though that would be less generally useful.

Hal
 
G

gabriele renzi

il Tue, 04 May 2004 00:05:05 -0500, Charles Comstock
Why is there only an IO::read, and no equivalent IO::write that takes a
filename, a string to write, and an optional offset in the file to start
writing at? Or better yet an IO::write(filename[,offset])= string

Agreed, that would be cool.
There is an implementation on the extensions page at rubyforge:

write(path, data)

Writes the given data to the given path and closes the file. This is
done in binary mode, complementing IO.read in standard Ruby.

[Source]

# File extensions/io.rb, line 24
def write(path, data)
File.open(path, "wb") do |file|
return file.write(data)
end
end

but as you see this misses the offset argument, that may be needed.
 
Y

Yukihiro Matsumoto

Hi,

In message "IO::write"

|Why is there only an IO::read, and no equivalent IO::write that takes a
|filename, a string to write, and an optional offset in the file to start
|writing at? Or better yet an IO::write(filename[,offset])= string to
|write to file. I don't know it seems useful to me for quickly writing a
|response out to a file anyone else?

IO::read is a equivalent of

IO::eek:pen(path, "r"){|f| f.read(*args)}

But IO::write that is

IO::eek:pen(path, "w"){|f| f.write(*args)}

is, for my eyes, useless for most of the cases. Besides that, I don't
feel it is intuitive that IO::write to "open, then write once, then
close". Even if we decide to implement the method, we should prepare
better name.

matz.
 
C

Charles Comstock

Yukihiro said:
Hi,

In message "IO::write"

|Why is there only an IO::read, and no equivalent IO::write that takes a
|filename, a string to write, and an optional offset in the file to start
|writing at? Or better yet an IO::write(filename[,offset])= string to
|write to file. I don't know it seems useful to me for quickly writing a
|response out to a file anyone else?

IO::read is a equivalent of

IO::eek:pen(path, "r"){|f| f.read(*args)}

But IO::write that is

IO::eek:pen(path, "w"){|f| f.write(*args)}

is, for my eyes, useless for most of the cases. Besides that, I don't
feel it is intuitive that IO::write to "open, then write once, then
close". Even if we decide to implement the method, we should prepare
better name.

matz.

The example listed is exactly the use case that I have littered about my
code quite liberally in one of my applications. Often it's not the best
approach to an application but sometimes it's nice to have quick file
records of application state. I suggested write because of the
orthagonality between it and read. Perhaps IO::save or IO::serialize.
Save might be interesting as you could alias load to do the same as
read. Would save/load be a more appropriate name?

Charles Comstock
 
R

Robert Klemme

Charles Comstock said:
Yukihiro said:
Hi,

In message "IO::write"

|Why is there only an IO::read, and no equivalent IO::write that takes a
|filename, a string to write, and an optional offset in the file to start
|writing at? Or better yet an IO::write(filename[,offset])= string to
|write to file. I don't know it seems useful to me for quickly writing a
|response out to a file anyone else?

IO::read is a equivalent of

IO::eek:pen(path, "r"){|f| f.read(*args)}

But IO::write that is

IO::eek:pen(path, "w"){|f| f.write(*args)}

Appending is probably useful, too

IO#write ~
IO::eek:pen(path, "w"){|f| f.write(*args)}

IO#append ~
IO::eek:pen(path, "a"){|f| f.write(*args)}
The example listed is exactly the use case that I have littered about my
code quite liberally in one of my applications. Often it's not the best
approach to an application but sometimes it's nice to have quick file
records of application state.

Did you consider using a logging framework? e.g. log4r
http://log4r.sourceforge.net/
I suggested write because of the
orthagonality between it and read. Perhaps IO::save or IO::serialize.
Save might be interesting as you could alias load to do the same as
read. Would save/load be a more appropriate name?

See above.

robert
 
G

Gavin Sinclair

In message "IO::write"
on 04/05/04, Charles Comstock <[email protected]> writes:
|Why is there only an IO::read, and no equivalent IO::write that takes a
|filename, a string to write, and an optional offset in the file to start
|writing at? Or better yet an IO::write(filename[,offset])= string to
|write to file. I don't know it seems useful to me for quickly writing a
|response out to a file anyone else?
IO::read is a equivalent of

IO::eek:pen(path, "r"){|f| f.read(*args)}

But IO::write that is

IO::eek:pen(path, "w"){|f| f.write(*args)}

is, for my eyes, useless for most of the cases.

You've stated in the past that you don't see much need for a "write at
once" operation like the above. I've needed it several times in the
past and believe I'll need it several times in the future.
Besides that, I don't feel it is intuitive that IO::write to "open,
then write once, then close". Even if we decide to implement the
method, we should prepare better name.

It's *very* intuitive to me, simply because IO.write is acting exactly
the opposite to IO.read.

I don't see what else a method named IO.write could do.

# Well, IO.read and IO.write should probably be methods on File, not
# IO, but that's not the point.

Cheers,
Gavin
 
G

Gavin Sinclair

[...] I suggested write because of the orthagonality between it and
read. Perhaps IO::save or IO::serialize. Save might be interesting
as you could alias load to do the same as read. Would save/load be a
more appropriate name?

Save and load!? I thought Ruby was based on Unix, not on Commodore 64!

Hmmmm....

IO.load "*", 8, 1

:)
 
A

Ara.T.Howard

Hi,

In message "IO::write"

|Why is there only an IO::read, and no equivalent IO::write that takes a
|filename, a string to write, and an optional offset in the file to start
|writing at? Or better yet an IO::write(filename[,offset])= string to
|write to file. I don't know it seems useful to me for quickly writing a
|response out to a file anyone else?

IO::read is a equivalent of

IO::eek:pen(path, "r"){|f| f.read(*args)}

But IO::write that is

IO::eek:pen(path, "w"){|f| f.write(*args)}

is, for my eyes, useless for most of the cases. Besides that, I don't
feel it is intuitive that IO::write to "open, then write once, then
close". Even if we decide to implement the method, we should prepare
better name.

matz.

IO#cat ??

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
===============================================================================
 
C

Charles Comstock

Robert Klemme wrote:

Did you consider using a logging framework? e.g. log4r
http://log4r.sourceforge.net/

Actually in this case a logging approach wouldn't have been quite what I
needed. I could have used it, but it was more that I was using lots of
small files to tag metainfo to a directory, so the log would not have
been the best solution.

Maybe it shouldn't be read, maybe it should be readfile and writefile,
as well as appendfile. I'm trying to find a nice orthogonal set that
still makes symantic sense.

Charlie
 
A

Ara.T.Howard

Robert Klemme wrote:



Actually in this case a logging approach wouldn't have been quite what I
needed. I could have used it, but it was more that I was using lots of
small files to tag metainfo to a directory, so the log would not have
been the best solution.

Maybe it shouldn't be read, maybe it should be readfile and writefile,
as well as appendfile. I'm trying to find a nice orthogonal set that
still makes symantic sense.

Charlie

how about

class File
def File.read(path)
...
end
def File.write(path, buf)
...
end
def File.append(path, buf)
...
end
end

??

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
===============================================================================
 

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

IO#write_nonblock(string, offset) useful? 8
Request data is empty 0
IO#gets with regex 1
IO problem 3
Chatbot 0
Counter-intuitive io vs no-io time readings 6
Relative seeks on string IO 2
IO#dup 4

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top