As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

Getting an error with the simplest Ruby program

PerpetualPerpetual Registered User regular
edited July 2011 in Help / Advice Forum
I started learning Ruby a few days ago, so I'm pretty new.

I installed Ruby 1.9.2 on my Win 7 machine. Created a simple analyzer.rb file. It has this one line:
File.open("text.txt").each {|line| puts line}

When I run the code, it gives me this error:
analyzer.rb:1:in `initialize': No such file or directory - text.txt (Errno::ENOENT)
from analyzer.rb:1:in `open'
from analyzer.rb:1:in `<main>'
>Exit code: 1

I don't get it. There is a text.txt file in the same directory as the analyzer.rb file. I also tried feeling the absolute path of the file, C:\Ruby192\text.txt but no dice. What am I missing?

Perpetual on

Posts

  • Options
    PaleCommanderPaleCommander Registered User regular
    edited July 2011
    I don't know any Ruby, but Googling the problem reveals that you want File.new, not File.open. File.open looks like it's only part of the operation you want to perform (opening a numeric file descriptor returned from another function).

    Reference: http://www.ruby-doc.org/core/classes/File.html#M000070

    PaleCommander on
    steam_sig.png
  • Options
    EchoEcho ski-bap ba-dapModerator mod
    edited July 2011
    What he said - File.new is what you want.

    It sounds a bit counterintuitive as if you're creating a new file, but what you're actually doing is creating a new File object to do stuff with, with the file name as an argument.

    Echo on
Sign In or Register to comment.