Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Simple editing. Works in IRB; not as script.

Reply
Thread Tools

Simple editing. Works in IRB; not as script.

 
 
Peter Bailey
Guest
Posts: n/a
 
      01-16-2007
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/.

 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      01-16-2007
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

 
Reply With Quote
 
 
 
 
Peter Bailey
Guest
Posts: n/a
 
      01-16-2007
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/.

 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      01-16-2007
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.")


The correct line of code is:

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

James Edward Gray II

 
Reply With Quote
 
Peter Bailey
Guest
Posts: n/a
 
      01-16-2007
Tamreen Khan wrote:
> On 1/16/07, James Edward Gray II <(E-Mail Removed)> 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/.

 
Reply With Quote
 
Peter Bailey
Guest
Posts: n/a
 
      01-16-2007

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getMethod() works and works not Alexander Burger Java 25 11-29-2010 06:33 PM
In a template, Eval () does not works in fields ofcontrols, it works abargaddon ASP .Net Web Controls 1 02-04-2008 09:16 PM
When I turn on my PC, it works, works, works. Problem! Fogar Computer Information 1 01-17-2006 12:57 AM
[py2exe.i18n] English works, German works, but not French. What do I miss? F. GEIGER Python 3 08-06-2004 10:01 AM
After rebooting my PC works, works, works! Antivirus problem? Adriano Computer Information 1 12-15-2003 05:30 AM



Advertisments