# File.write a long line

**URL:** <https://www.ruby-forum.com/t/file-write-a-long-line/168149>\
**Category:** Ruby\
**Created:** [May 16, 2009, 9:36pm UTC](https://www.ruby-forum.com/t/file-write-a-long-line/168149 "2009-05-16T21:36:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 16, 2009, 9:36pm UTC](https://www.ruby-forum.com/t/file-write-a-long-line/168149/1 "2009-05-16T21:36:27Z")

</div>

I notice you can do this with print:

print “blah blah”,  
“and so on”

but not with File.write, except

file.write “blah blah /  
and so on”

which is no good cause it includes all the whitespace. I guess I can  
live with

file.write “blah blah”  
file.write “and so on”

but I thot I’d check and see if there is not some wierd combination of +  
, / etc that would allow me to break the line, as I imagine the same  
logic will apply in other ruby methods.

Thanks in advance! Ruby seems very nice, I am happy.

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 16, 2009, 11:01pm UTC](https://www.ruby-forum.com/t/file-write-a-long-line/168149/2 "2009-05-16T23:01:02Z")

</div>

On 16.05.2009 21:36, Mk 27 wrote:

> which is no good cause it includes all the whitespace. I guess I can  
> live with
> 
> file.write “blah blah”  
> file.write “and so on”

> but I thot I’d check and see if there is not some wierd combination of +  
> , / etc that would allow me to break the line, as I imagine the same  
> logic will apply in other ruby methods.

You can do

file.write "blah blah " +  
“and so on”

file.write "blah blah "   
“and so on”

I myself use these rules of thumb to decide which method to use:

1. printing of text: puts, print, printf
2. output of binary or chunked data: write

E.g. for copying data from one file to another, I’d do

buffer = “”  
while io1.read(1024, buffer)  
io2.write buffer  
end

> Thanks in advance! Ruby seems very nice, I am happy.

Good!

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 17, 2009, 2:55pm UTC](https://www.ruby-forum.com/t/file-write-a-long-line/168149/3 "2009-05-17T14:55:27Z")

</div>

Hi,

Am Sonntag, 17. Mai 2009, 06:00:04 +0900 schrieb Robert K.:

> On 16.05.2009 21:36, Mk 27 wrote:
> 
> > file.write “blah blah /  
> > and so on”  
> > which is no good cause it includes all the whitespace.
> 
> file.write "blah blah " +  
> “and so on”  
> file.write "blah blah "   
> “and so on”

Maybe here documents?

file.write \<\<EOT.chomp  
blah blah   
and so on  
EOT

Bertram

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 18, 2009, 8:36am UTC](https://www.ruby-forum.com/t/file-write-a-long-line/168149/4 "2009-05-18T08:36:56Z")

</div>

2009/5/17 Mk 27 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Bertram S. wrote:
> 
> > Maybe here documents?
> 
> No, just to save a little bit of typing a line with a bunch of long  
> object.hash[key] variables in it. But since the logic must applies to  
> other methods, I figured I might as well start “saving” now.
> 
> Anyway, it seems the + version works but the / ignores the next line if  
> the quotes are closed.

Note that for the continuation you need a bachslash "" and not a  
forward slash “/”! The difference between the two variants is that  
the continuation forms one long string while the plus version creates  
several strings which are combined at runtime. It seems that in your  
case the version with “+” may be better as you seem to pull your  
strings from somewhere else (i.e. they are not constant). Can you  
show the real code?

Cheers

robert

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 17, 2009, 7:56pm UTC](https://www.ruby-forum.com/t/file-write-a-long-line/168149/5 "2009-05-17T19:56:07Z")

</div>

Bertram S. wrote:

> Maybe here documents?

No, just to save a little bit of typing a line with a bunch of long  
object.hash[key] variables in it. But since the logic must applies to  
other methods, I figured I might as well start “saving” now.

Anyway, it seems the + version works but the / ignores the next line if  
the quotes are closed.
