[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

File.puts or File.write ?

Joe Ruby

10/13/2006 9:55:00 PM

For general purpose file writing, does it matter whether puts or write
is used? From the docs, it seems like puts should be used for strings,
while write can be used for anything. I'm not really sure what
difference there is between the two, but the docs say something about
puts adding newlines or something.

Thanks,
Joe

--
Posted via http://www.ruby-....

4 Answers

Justin Collins

10/14/2006 12:23:00 AM

0

Joe Ruby MUDCRAP-CE wrote:
> For general purpose file writing, does it matter whether puts or write
> is used? From the docs, it seems like puts should be used for strings,
> while write can be used for anything. I'm not really sure what
> difference there is between the two, but the docs say something about
> puts adding newlines or something.
>
> Thanks,
> Joe
>
>

Yes, using puts adds a newline to the end of the output, while write and
print do not. So, yes, it can matter a great deal which one you use.

-Justin

Wilson Bilkovich

10/14/2006 12:26:00 AM

0

On 10/13/06, Justin Collins <collinsj@seattleu.edu> wrote:
> Joe Ruby MUDCRAP-CE wrote:
> > For general purpose file writing, does it matter whether puts or write
> > is used? From the docs, it seems like puts should be used for strings,
> > while write can be used for anything. I'm not really sure what
> > difference there is between the two, but the docs say something about
> > puts adding newlines or something.
> >
> > Thanks,
> > Joe
> >
> >
>
> Yes, using puts adds a newline to the end of the output, while write and
> print do not. So, yes, it can matter a great deal which one you use.
>

puts only does that when you're not in binary mode, though. I have yet
to use 'write' for anything, at least as far as I can recall.

Jano Svitok

10/14/2006 7:09:00 AM

0

On 10/14/06, Wilson Bilkovich <wilsonb@gmail.com> wrote:
> On 10/13/06, Justin Collins <collinsj@seattleu.edu> wrote:
> > Joe Ruby MUDCRAP-CE wrote:
> > > For general purpose file writing, does it matter whether puts or write
> > > is used? From the docs, it seems like puts should be used for strings,
> > > while write can be used for anything. I'm not really sure what
> > > difference there is between the two, but the docs say something about
> > > puts adding newlines or something.
> > >
> > > Thanks,
> > > Joe
> > >
> > >
> >
> > Yes, using puts adds a newline to the end of the output, while write and
> > print do not. So, yes, it can matter a great deal which one you use.
> >
>
> puts only does that when you're not in binary mode, though. I have yet
> to use 'write' for anything, at least as far as I can recall.

And IIRC write is atomic - 'puts' consists of two 'write's, so there
may be a thread switch between them (at least on current
implementation). In other words, if writing to a file from more
threads, 'write (string + "\n")' should be always right, while using
'puts string' the end-of-lines might be crossed. (string1 string2 eol1
eol2).

Robert Klemme

10/14/2006 9:13:00 AM

0

Joe Ruby MUDCRAP-CE wrote:
> For general purpose file writing, does it matter whether puts or write
> is used? From the docs, it seems like puts should be used for strings,
> while write can be used for anything. I'm not really sure what
> difference there is between the two, but the docs say something about
> puts adding newlines or something.

I tend to view #puts and #print as printing methodsi, i.e. for textual
output. Note also that they accept multiple parameters.

#write on the other hand is more low level and is actually sending off
the string it gets to the underlying stream whereas #puts applies some
modifications (newline, special treatment of things that implement
#to_ary etc.)

Typically I use #write for stream copy operations like

File.open("foo", "rb") do |in|
File.open("bar", "wb") do |out|
while ( buffer = in.read( 1024 ) )
out.write( buffer )
end
end
end

Kind regards

robert