Simple editing. Works in IRB; not as script.

P

Peter Bailey

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
 
P

Peter Bailey

James said:
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.")
 
J

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.")

The correct line of code is:

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

James Edward Gray II
 
P

Peter Bailey

Tamreen said:
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.
 
P

Peter Bailey

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top