# Simple editing. Works in IRB; not as script

**URL:** <https://rubytalk.org/t/simple-editing-works-in-irb-not-as-script/34512>\
**Category:** ruby-talk\
**Created:** [16 January 2007 17:24 UTC](https://rubytalk.org/t/simple-editing-works-in-irb-not-as-script/34512 "2007-01-16T17:24:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)\
**Post date:** [16 January 2007 17:24 UTC](https://rubytalk.org/t/simple-editing-works-in-irb-not-as-script/34512/1 "2007-01-16T17:24:01Z")

</div>

Hello,  
I'm trying to create a simple editing template for myself, just some  
code that I can use for future editing needs, where I open a file,  
change stuff, and then save the file. I created this simple little  
script, but, it doesn't work when I run it. It complains about line 3. I  
get an exception with "wrong number of arguments (0 for 1)." But, when I  
do this script, line for line, in IRB, I have no problems.

Dir.chdir("C:/documents and settings/pb4072/my documents/scripts/ruby")  
contents = File.read{"test1.txt"}

#The file is just a bunch of repeated "The quick brown fox..." phrases.

contents.gsub!(/fox/, "horse")  
File.open("test2.txt", "w") { |f| f.print contents }

Thanks,  
Peter

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [16 January 2007 17:29 UTC](https://rubytalk.org/t/simple-editing-works-in-irb-not-as-script/34512/2 "2007-01-16T17:29:53Z")

</div>

That's a typo. You want (...) instead of {...}.

James Edward Gray II

> **···**
>
> On Jan 16, 2007, at 11:24 AM, Peter Bailey wrote:
> 
> > contents = File.read{"test1.txt"}

---

<div class="post-metadata">

**Author:** ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)\
**Post date:** [16 January 2007 17:39 UTC](https://rubytalk.org/t/simple-editing-works-in-irb-not-as-script/34512/3 "2007-01-16T17:39:06Z")

</div>

James Gray wrote:

> **···**
>
> > On Jan 16, 2007, at 11:24 AM, Peter Bailey wrote:
> > 
> > > contents = File.read{"test1.txt"}
> > 
> > That's a typo. You want (...) instead of {...}.
> > 
> > James Edward Gray II
> 
> Duh, sorry James, but, I don't see what you mean. I don't have any (...)  
> data in there. The test file I made is named "test1.txt.")
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [16 January 2007 18:01 UTC](https://rubytalk.org/t/simple-editing-works-in-irb-not-as-script/34512/4 "2007-01-16T18:01:28Z")

</div>

The correct line of code is:

&nbsp;&nbsp;&nbsp;contents = File.read("test1.txt")

James Edward Gray II

> **···**
>
> On Jan 16, 2007, at 11:39 AM, Peter Bailey wrote:
> 
> > James Gray wrote:
> > 
> > > On Jan 16, 2007, at 11:24 AM, Peter Bailey wrote:
> > > 
> > > > contents = File.read{"test1.txt"}
> > > 
> > > That's a typo. You want (...) instead of {...}.
> > > 
> > > James Edward Gray II
> > 
> > Duh, sorry James, but, I don't see what you mean. I don't have any (...)  
> > data in there. The test file I made is named "test1.txt.")

---

<div class="post-metadata">

**Author:** ![Tamreen\_Khan](https://avatars.discourse-cdn.com/v4/letter/t/59ef9b/32.png) [@Tamreen\_Khan](https://rubytalk.org/u/Tamreen_Khan)\
**Post date:** [16 January 2007 19:00 UTC](https://rubytalk.org/t/simple-editing-works-in-irb-not-as-script/34512/5 "2007-01-16T19:00:06Z")

</div>

You need to pass File.read a simple method argument, which goes inside  
parentheses, you don't need a block.

> **···**
>
> On 1/16/07, James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > On Jan 16, 2007, at 11:39 AM, Peter Bailey wrote:
> > 
> > \> James Gray wrote:  
> > \>\> On Jan 16, 2007, at 11:24 AM, Peter Bailey wrote:  
> > \>\>  
> > \>\>\> contents = File.read{"test1.txt"}  
> > \>\>  
> > \>\> That's a typo. You want (...) instead of {...}.  
> > \>\>  
> > \>\> James Edward Gray II  
> > \>  
> > \> Duh, sorry James, but, I don't see what you mean. I don't have any  
> > \> (...)  
> > \> data in there. The test file I made is named "test1.txt.")

---

<div class="post-metadata">

**Author:** ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)\
**Post date:** [16 January 2007 19:07 UTC](https://rubytalk.org/t/simple-editing-works-in-irb-not-as-script/34512/6 "2007-01-16T19:07:29Z")

</div>

Tamreen Khan wrote:

> **···**
>
> > On 1/16/07, James Edward Gray II \<james@grayproductions.net\> wrote:
> > 
> > > \>\> James Edward Gray II  
> > > \>  
> > > \> Duh, sorry James, but, I don't see what you mean. I don't have any  
> > > \> (...)  
> > > \> data in there. The test file I made is named "test1.txt.")
> > 
> > You need to pass File.read a simple method argument, which goes inside  
> > parentheses, you don't need a block.
> 
> I don't understand. My line says this:
> 
> contents = File.read{"test1.txt"}
> 
> I'm using parentheses, no block. I've done this in a number of other  
> scripts, and, they work fine.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Tamreen\_Khan](https://avatars.discourse-cdn.com/v4/letter/t/59ef9b/32.png) [@Tamreen\_Khan](https://rubytalk.org/u/Tamreen_Khan)\
**Post date:** [16 January 2007 19:19 UTC](https://rubytalk.org/t/simple-editing-works-in-irb-not-as-script/34512/7 "2007-01-16T19:19:33Z")

</div>

> Tamreen Khan wrote:  
> \>\> \>\> James Edward Gray II  
> \>\> \>  
> \>\> \> Duh, sorry James, but, I don't see what you mean. I don't have any  
> \>\> \> (...)  
> \>\> \> data in there. The test file I made is named "test1.txt.")  
> \>  
> \> You need to pass File.read a simple method argument, which goes inside  
> \> parentheses, you don't need a block.  
> \>  
> \>I don't understand. My line says this:  
> \>  
> \>contents = File.read{"test1.txt"}  
> \>  
> \>I'm using parentheses, no block. I've done this in a number of other  
> \>scripts, and, they work fine.

Those aren't parentheses, they're braces, which means you're passing a block  
to it.  
You have to use ( instead of { .

contents= File.read("test1.txt")

> **···**
>
> On 1/16/07, Peter Bailey \<pbailey@bna.com\> wrote:
> 
> > \> On 1/16/07, James Edward Gray II \<james@grayproductions.net\> wrote:

---

<div class="post-metadata">

**Author:** ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)\
**Post date:** [16 January 2007 19:23 UTC](https://rubytalk.org/t/simple-editing-works-in-irb-not-as-script/34512/8 "2007-01-16T19:23:42Z")

</div>

> Those aren't parentheses, they're braces, which means you're passing a  
> block  
> to it.  
> You have to use ( instead of { .
> 
> contents= File.read("test1.txt")

Yes, I'm an idiot. Thank you very much. I need really big glasses.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
