# Puts vs. write for writing to a file

**URL:** <https://www.ruby-forum.com/t/puts-vs-write-for-writing-to-a-file/117381>\
**Category:** Ruby\
**Created:** [October 16, 2007, 2:26pm UTC](https://www.ruby-forum.com/t/puts-vs-write-for-writing-to-a-file/117381 "2007-10-16T14:26:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![randy\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@randy\_k](https://www.ruby-forum.com/u/randy_k)\
**Post date:** [October 16, 2007, 2:26pm UTC](https://www.ruby-forum.com/t/puts-vs-write-for-writing-to-a-file/117381/1 "2007-10-16T14:26:55Z")

</div>

I’ve done some googling, some looking in the pickaxe(2) book (using the  
index), and some experimenting in irb, but I can’t readily see the  
difference  
between puts and write (except that write doesn’t work in all cases in  
irb:)

irb(main):044:0\> write “\nmorF\n\n”  
NoMethodError: undefined method `write’ for main:Object

So what is the difference (specifically for the case of writing to a  
file)  
and/or when should I use one vs. the other?

Randy K.

---

<div class="post-metadata">

**Author:** ![randy\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@randy\_k](https://www.ruby-forum.com/u/randy_k)\
**Post date:** [October 16, 2007, 2:32pm UTC](https://www.ruby-forum.com/t/puts-vs-write-for-writing-to-a-file/117381/2 "2007-10-16T14:32:16Z")

</div>

Randy K. wrote:

> I’ve done some googling, some looking in the pickaxe(2) book (using the  
> index), and some experimenting in irb, but I can’t readily see the difference  
> between puts and write (except that write doesn’t work in all cases in irb:)
> 
> irb(main):044:0\> write “\nmorF\n\n”  
> NoMethodError: undefined method `write’ for main:Object
> 
> So what is the difference (specifically for the case of writing to a file)  
> and/or when should I use one vs. the other?

On a File object (actually, they’re defined on IO), write just writes  
the string given to the file unchanged. IO#puts adds a newline at the  
end of the string if there isn’t one already.

---

<div class="post-metadata">

**Author:** ![randy\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@randy\_k](https://www.ruby-forum.com/u/randy_k)\
**Post date:** [October 16, 2007, 7:00pm UTC](https://www.ruby-forum.com/t/puts-vs-write-for-writing-to-a-file/117381/3 "2007-10-16T19:00:33Z")

</div>

On 10/16/07, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> On a File object (actually, they’re defined on IO), write just writes  
> the string given to the file unchanged. IO#puts adds a newline at the  
> end of the string if there isn’t one already.  
> Furthermore puts formats array instances as follows

array\_instance.join(“\n”).

Robert

---

<div class="post-metadata">

**Author:** ![randy\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@randy\_k](https://www.ruby-forum.com/u/randy_k)\
**Post date:** [October 16, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/puts-vs-write-for-writing-to-a-file/117381/4 "2007-10-16T16:47:32Z")

</div>

On Tuesday 16 October 2007 08:31 am, Alex Y. wrote:

> On a File object (actually, they’re defined on IO), write just writes  
> the string given to the file unchanged. IO#puts adds a newline at the  
> end of the string if there isn’t one already.

Ahha, thank you very much!

(Now, knowing that, I can see it in the pickaxe-- I guess there were too  
many  
other wording differences between the two for me to quickly spot the  
significant (for me) difference. For example, puts (on page 512) is  
described in terms of writing an object, while write (page 515) is  
described  
in terms of writing a string.)

Randy K.

---

<div class="post-metadata">

**Author:** ![randy\_k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/258eb7/32.png) [@randy\_k](https://www.ruby-forum.com/u/randy_k)\
**Post date:** [October 16, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/puts-vs-write-for-writing-to-a-file/117381/5 "2007-10-16T19:51:32Z")

</div>

On Tue, Oct 16, 2007 at 09:31:27PM +0900, Alex Y. wrote:

> > and/or when should I use one vs. the other?
> 
> On a File object (actually, they’re defined on IO), write just writes  
> the string given to the file unchanged. IO#puts adds a newline at the  
> end of the string if there isn’t one already.

I guess the next question, then, is “What’s the difference between print  
and write for writing to a file?”

Trying to answer my own question with irb, I see that a successful write  
has a return value equal to the number of characters it writes to the  
file, while a successful print returns nil. Is that the only difference  
in a file writing context?
