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?

Charles Comstock

Charles Comstock wrote:

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

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.

···

il Tue, 04 May 2004 00:05:05 -0500, Charles Comstock cc1@cec.wustl.edu ha scritto::

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

Hi,

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::open(path, “r”){|f| f.read(*args)}

But IO::write that is

IO::open(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.
···

In message “IO::write” on 04/05/04, Charles Comstock cc1@cec.wustl.edu writes:

Yukihiro Matsumoto wrote:

Hi,

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::open(path, “r”){|f| f.read(*args)}

But IO::write that is

IO::open(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

···

In message “IO::write” > on 04/05/04, Charles Comstock cc1@cec.wustl.edu writes:

Hi,

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::open(path, “r”){|f| f.read(*args)}

But IO::write that is

IO::open(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

···

On Tuesday, May 4, 2004, 6:42:35 PM, Yukihiro wrote:

In message “IO::write” > on 04/05/04, Charles Comstock cc1@cec.wustl.edu writes:

IO#cat ??

-a

···

On Tue, 4 May 2004, Yukihiro Matsumoto wrote:

Hi,

In message “IO::write” > on 04/05/04, Charles Comstock cc1@cec.wustl.edu 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::open(path, “r”){|f| f.read(*args)}

But IO::write that is

IO::open(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.

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================

“Charles Comstock” cc1@cec.wustl.edu schrieb im Newsbeitrag
news:c77oll$hog$1@newsreader.wustl.edu…

Yukihiro Matsumoto wrote:

Hi,

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::open(path, “r”){|f| f.read(*args)}

But IO::write that is

IO::open(path, “w”){|f| f.write(*args)}

Appending is probably useful, too

IO#write ~
IO::open(path, “w”){|f| f.write(*args)}

IO#append ~
IO::open(path, “a”){|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.

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
···

In message “IO::write” > > on 04/05/04, Charles Comstock cc1@cec.wustl.edu writes:

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

Hmmmm…

IO.load “*”, 8, 1

:slight_smile:

···

On Tuesday, May 4, 2004, 7:49:04 PM, Charles wrote:

[…] 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?

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

how about

class File
def File.read(path)

end
def File.write(path, buf)

end
def File.append(path, buf)

end
end

??

-a

···

On Tue, 4 May 2004, Charles Comstock wrote:

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

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================

Hear, hear!!! :smiley:

···

On Wednesday, May 5, 2004, 8:14:00 AM, Ara.T.Howard wrote:

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