# Unable to write to file... (example from "pickaxe book", sec

**URL:** <https://www.ruby-forum.com/t/unable-to-write-to-file-example-from-pickaxe-book-sec/52814>\
**Category:** Ruby\
**Created:** [January 19, 2006, 12:28pm UTC](https://www.ruby-forum.com/t/unable-to-write-to-file-example-from-pickaxe-book-sec/52814 "2006-01-19T12:28:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 19, 2006, 12:28pm UTC](https://www.ruby-forum.com/t/unable-to-write-to-file-example-from-pickaxe-book-sec/52814/1 "2006-01-19T12:28:18Z")

</div>

Following the example from “pickaxe book”, second edition, p128

#!/usr/bin/ruby  
File.open(“testfile”, “r”) do |file|  
while line = file.gets  
puts line  
end  
end

produces  
copy.rb:2:in `initialize’: No such file or directory - testfile  
(Errno::ENOENT)  
from copy.rb:2

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 19, 2006, 12:34pm UTC](https://www.ruby-forum.com/t/unable-to-write-to-file-example-from-pickaxe-book-sec/52814/2 "2006-01-19T12:34:20Z")

</div>

On Thursday 19 January 2006 11:49, John M. wrote:

> copy.rb:2:in `initialize’: No such file or directory - testfile  
> (Errno::ENOENT) from copy.rb:2

The problem is clear. You are trying to read a file which does not  
exist. So,  
to solve the `problem’, you create a file named testfile, put some text  
in  
it, and there you go,

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 19, 2006, 12:34pm UTC](https://www.ruby-forum.com/t/unable-to-write-to-file-example-from-pickaxe-book-sec/52814/3 "2006-01-19T12:34:21Z")

</div>

John M. wrote:

> copy.rb:2:in `initialize’: No such file or directory - testfile (Errno::ENOENT)  
> from copy.rb:2

John, you need the file named “testfile” to exist before you can run  
that code. It can’t open a file  
if it doesn’t exist!

Zach

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 19, 2006, 12:58pm UTC](https://www.ruby-forum.com/t/unable-to-write-to-file-example-from-pickaxe-book-sec/52814/4 "2006-01-19T12:58:28Z")

</div>

Thanks! The light shines. So how can i -write- to a file using the code  
below as an example?

On Thu, 19 Jan 2006 19:56:55 +0900

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 19, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/unable-to-write-to-file-example-from-pickaxe-book-sec/52814/5 "2006-01-19T18:05:40Z")

</div>

John M. wrote:

> Thanks! The light shines. So how can i -write- to a file using the code  
> below as an example?

Open a file for writing

File.open( “infile”, “r” ) | in |  
File.open( “outfile”, “w” ) | out |  
while line = in.gets  
out.print line  
end  
end  
end

See the docs for IO which explain all the mode flags (“r”, “w”, “a”,  
etc.).

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 3, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/unable-to-write-to-file-example-from-pickaxe-book-sec/52814/6 "2006-11-03T18:24:13Z")

</div>

Mike F. wrote:

> John M. wrote:
> 
> > Thanks! The light shines. So how can i -write- to a file using the code  
> > below as an example?
> 
> Open a file for writing
> 
> File.open( “infile”, “r” ) | in |  
> File.open( “outfile”, “w” ) | out |  
> while line = in.gets  
> out.print line  
> end  
> end  
> end
> 
> See the docs for IO which explain all the mode flags (“r”, “w”, “a”,  
> etc.).

Do you think this script work?

Li

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 3, 2006, 8:34pm UTC](https://www.ruby-forum.com/t/unable-to-write-to-file-example-from-pickaxe-book-sec/52814/7 "2006-11-03T20:34:04Z")

</div>

Li Chen wrote:

> Mike F. wrote:
> 
> > John M. wrote:
> > 
> > > Thanks! The light shines. So how can i -write- to a file using the code  
> > > below as an example?
> > 
> > Open a file for writing
> > 
> > File.open( “infile”, “r” ) | in |  
> > File.open( “outfile”, “w” ) | out |  
> > while line = in.gets  
> > out.print line  
> > end  
> > end  
> > end
> > 
> > See the docs for IO which explain all the mode flags (“r”, “w”, “a”,  
> > etc.).
> 
> Do you think this script work?

As is no (I blame lack of caffeine and/or sleep); but with “do” inserted  
in the right two places and the reserved word “in” replaced with  
something that’s not a reserved word ( say “inf” ) it works just fine.

One should take all example code posted in haste to mailing lists with a  
grain of salt.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [November 7, 2006, 10:59am UTC](https://www.ruby-forum.com/t/unable-to-write-to-file-example-from-pickaxe-book-sec/52814/8 "2006-11-07T10:59:16Z")

</div>

On 03/11/06, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end
> 
> Do you think this script work?

It fails because “in” is a reserved word in Ruby and cannot be used as  
a variable name. Try changing the two instances of “in” to “inp” and  
it will work.
