Simple editing. Works in IRB; not as script.

Discussion in 'Ruby' started by Peter Bailey, Jan 16, 2007.

  1. Peter Bailey

    Peter Bailey Guest

    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/.
     
    Peter Bailey, Jan 16, 2007
    #1
    1. Advertising

  2. 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
     
    James Edward Gray II, Jan 16, 2007
    #2
    1. Advertising

  3. Peter Bailey

    Peter Bailey Guest

    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/.
     
    Peter Bailey, Jan 16, 2007
    #3
  4. 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
     
    James Edward Gray II, Jan 16, 2007
    #4
  5. Peter Bailey

    Peter Bailey Guest

    Tamreen Khan wrote:
    > On 1/16/07, James Edward Gray II <> 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/.
     
    Peter Bailey, Jan 16, 2007
    #5
  6. Peter Bailey

    Peter Bailey Guest


    >
    > 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/.
     
    Peter Bailey, Jan 16, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel VanderWerf

    command line editing in irb under MSYS

    Joel VanderWerf, Nov 25, 2003, in forum: Ruby
    Replies:
    1
    Views:
    143
    Joel VanderWerf
    Nov 25, 2003
  2. Sam Stephenson
    Replies:
    1
    Views:
    266
    Andrew Walrond
    Jun 18, 2005
  3. Replies:
    1
    Views:
    179
    Florian GroƟ
    Oct 26, 2005
  4. anne001
    Replies:
    1
    Views:
    310
    anne001
    Jun 27, 2006
  5. Deepak Kannan
    Replies:
    1
    Views:
    115
    Jonas Pfenniger (zimbatm)
    Jan 20, 2011
Loading...

Share This Page