Non-Hard Coded File.open(newFile)

Discussion in 'Ruby' started by Grant Curell, Oct 23, 2010.

  1. Grant Curell

    Grant Curell Guest

    Like a lot of people I'm new to Ruby and I'm trying to do something I
    thought would be pretty simple. I want the user to give me input in the
    form of a filename and then subsequently open the file. Here is what I
    have so far:

    class FileHandler

    def initialize()
    @fileToParse = fileToParse #I get user input in the form of gets in
    #another class.
    if File.new(fileToParse)
    puts 'File successfully opened.'
    else
    puts 'File failed to open.'
    Kernel.exit

    end
    end
    end

    However, that won't work. Doing something like
    File.new("C:/Users/grant/Desktop/test.txt") works just fine though. The
    way I'm currently doing it throws the following error:

    F:/Programming/eclipseWorkspace/CSE_655/file_handler.rb:8:in
    `initialize': Invalid argument - C:/Users/grant/Desktop/test.txt
    (Errno::EINVAL)

    --
    Posted via http://www.ruby-forum.com/.
     
    Grant Curell, Oct 23, 2010
    #1
    1. Advertising

  2. On Sat, Oct 23, 2010 at 6:35 AM, Grant Curell <> wrote=
    :
    > Like a lot of people I'm new to Ruby and I'm trying to do something I
    > thought would be pretty simple. I want the user to give me input in the
    > form of a filename and then subsequently open the file. Here is what I
    > have so far:


    > =A0def initialize()
    > =A0 =A0@fileToParse =3D fileToParse #I get user input in the form of gets=

    in
    > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #another clas=

    s.

    Are you removing the newline at the end of the string from `gets` ?

    > F:/Programming/eclipseWorkspace/CSE_655/file_handler.rb:8:in
    > `initialize': Invalid argument - C:/Users/grant/Desktop/test.txt


    Probably actually "C:/Users/grant/Desktop/test.txt\n" right here ^

    > (Errno::EINVAL)


    HTH,
    --=20
    Hassan Schroeder ------------------------
    twitter: @hassan
     
    Hassan Schroeder, Oct 23, 2010
    #2
    1. Advertising

  3. On Saturday 23 October 2010, Grant Curell wrote:
    > |Like a lot of people I'm new to Ruby and I'm trying to do something I
    > |thought would be pretty simple. I want the user to give me input in the
    > |form of a filename and then subsequently open the file. Here is what I
    > |have so far:
    > |
    > |class FileHandler
    > |
    > | def initialize()
    > | @fileToParse = fileToParse #I get user input in the form of gets in
    > | #another class.
    > | if File.new(fileToParse)
    > | puts 'File successfully opened.'
    > | else
    > | puts 'File failed to open.'
    > | Kernel.exit
    > |
    > | end
    > | end
    > |end
    > |
    > |However, that won't work. Doing something like
    > |File.new("C:/Users/grant/Desktop/test.txt") works just fine though. The
    > |way I'm currently doing it throws the following error:
    > |
    > |F:/Programming/eclipseWorkspace/CSE_655/file_handler.rb:8:in
    > |`initialize': Invalid argument - C:/Users/grant/Desktop/test.txt
    > |(Errno::EINVAL)


    Your code seems mostly correct, but I don't exactly understand what
    fileToParse is. Is it a call to a fileToParse method (which in turn uses geets
    to retrieve the file name) or a variable? In the first case, I guess the if
    line should be

    if File.new(@fileToParse) #note the @

    Otherwise, the user will be asked to enter the file name twice.

    If fileToParse is a variable, instead, where does it come from?


    Also, it would be useful if you pointed out which is line 8 on your program.
    If I assume the first line is

    class FileHandler

    then line 8 is 'else', which I doubt could give you such an error.

    Stefano
     
    Stefano Crocco, Oct 23, 2010
    #3
  4. Grant Curell

    Grant Curell Guest

    Thank you so much Hassan, that fix seemed so obvious after you said it
    lol. I just changed line 8 to if File.new(fileToParse.delete "\n") and
    it worked just fine. Not sure if I should ask this here but just out of
    curiosity does the debugger for 1.9.2 not work? I'm running the eclipse
    plug in right now and whenever I try to debug it just vomits all over
    itself. Browsed around the web and saw it mentioned a couple of times.

    --
    Posted via http://www.ruby-forum.com/.
     
    Grant Curell, Oct 23, 2010
    #4
  5. On Sat, Oct 23, 2010 at 8:17 AM, Grant Curell <> wrote:
    > Thank you so much Hassan, that fix seemed so obvious after you said it
    > lol. I just changed line 8 to if File.new(fileToParse.delete "\n")


    Good, but you should probably look at fileToParse.chomp for a more
    platform-agnostic approach :)

    > Not sure if I should ask this here but just out of
    > curiosity does the debugger for 1.9.2 not work? I'm running the eclipse
    > plug in right now


    Sorry, no idea about Eclipse.

    --
    Hassan Schroeder ------------------------
    twitter: @hassan
     
    Hassan Schroeder, Oct 23, 2010
    #5
  6. Grant Curell

    w_a_x_man Guest

    On Oct 23, 8:35 am, Grant Curell <> wrote:
    > Like a lot of people I'm new to Ruby and I'm trying to do something I
    > thought would be pretty simple. I want the user to give me input in the
    > form of a filename and then subsequently open the file. Here is what I
    > have so far:
    >
    > class FileHandler
    >
    >   def initialize()
    >     @fileToParse = fileToParse #I get user input in the form of gets in


    Usually after gets you want to use strip to remove all leading
    and trailing whitespace:

    file_name = gets.strip
     
    w_a_x_man, Oct 23, 2010
    #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. Luis Esteban Valencia
    Replies:
    1
    Views:
    528
    Curt_C [MVP]
    Jan 6, 2005
  2. Luke Airig
    Replies:
    1
    Views:
    591
    Dimitre Novatchev
    Dec 24, 2003
  3. rodchar
    Replies:
    2
    Views:
    369
    rodchar
    Jul 1, 2008
  4. Aidan Gauland

    Refactoring hard-coded values

    Aidan Gauland, Jun 29, 2011, in forum: XML
    Replies:
    1
    Views:
    1,331
    Joe Kesselman
    Jun 30, 2011
  5. Francois Massion
    Replies:
    6
    Views:
    660
    Ted Zlatanov
    Mar 27, 2012
Loading...

Share This Page