# Simple editing. Works in IRB; not as script

**URL:** <https://www.ruby-forum.com/t/simple-editing-works-in-irb-not-as-script/81981>\
**Category:** Ruby\
**Created:** [January 16, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/simple-editing-works-in-irb-not-as-script/81981 "2007-01-16T18:23:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 16, 2007, 6:23pm UTC](https://www.ruby-forum.com/t/simple-editing-works-in-irb-not-as-script/81981/1 "2007-01-16T18:23:44Z")

</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

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 16, 2007, 6:30pm UTC](https://www.ruby-forum.com/t/simple-editing-works-in-irb-not-as-script/81981/2 "2007-01-16T18:30:55Z")

</div>

On Jan 16, 2007, at 11:24 AM, Peter B. wrote:

> contents = File.read{“test1.txt”}

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

James Edward G. II

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 16, 2007, 6:39pm UTC](https://www.ruby-forum.com/t/simple-editing-works-in-irb-not-as-script/81981/3 "2007-01-16T18:39:01Z")

</div>

James G. wrote:

> On Jan 16, 2007, at 11:24 AM, Peter B. wrote:
> 
> > contents = File.read{“test1.txt”}
> 
> That’s a typo. You want (…) instead of {…}.
> 
> James Edward G. 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:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 16, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/simple-editing-works-in-irb-not-as-script/81981/4 "2007-01-16T19:01:59Z")

</div>

On Jan 16, 2007, at 11:39 AM, Peter B. wrote:

> (…)  
> data in there. The test file I made is named “test1.txt.”)

The correct line of code is:

contents = File.read(“test1.txt”)

James Edward G. II

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 16, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/simple-editing-works-in-irb-not-as-script/81981/5 "2007-01-16T20:01:16Z")

</div>

On 1/16/07, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > James Edward G. 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.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 16, 2007, 8:20pm UTC](https://www.ruby-forum.com/t/simple-editing-works-in-irb-not-as-script/81981/6 "2007-01-16T20:20:21Z")

</div>

On 1/16/07, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 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”)

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 16, 2007, 8:07pm UTC](https://www.ruby-forum.com/t/simple-editing-works-in-irb-not-as-script/81981/7 "2007-01-16T20:07:20Z")

</div>

Tamreen Khan wrote:

> On 1/16/07, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > > James Edward G. 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.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [January 16, 2007, 8:23pm UTC](https://www.ruby-forum.com/t/simple-editing-works-in-irb-not-as-script/81981/8 "2007-01-16T20:23:38Z")

</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.
