Is there a clash between gets and ARGV?

Discussion in 'Ruby' started by ycsunil, Feb 20, 2008.

  1. ycsunil

    ycsunil Guest

    Hello all,

    Here is my small ruby program:

    if (ARGV[0] == 'add')
    puts 'input something'
    gt = gets
    puts gt
    else
    puts 'fail'
    end

    If I try to execute above program am getting below error:

    C:\Documents and Settings\sunilc\Desktop>ruby test.rb 'add'
    input something
    test.rb:3:in `gets': No such file or directory - add (Errno::ENOENT)
    from test.rb:3

    Please let me know why is this error coming?

    -Sunil
     
    ycsunil, Feb 20, 2008
    #1
    1. Advertising

  2. ycsunil wrote:
    > C:\Documents and Settings\sunilc\Desktop>ruby test.rb 'add'
    > input something
    > test.rb:3:in `gets': No such file or directory - add (Errno::ENOENT)
    > =A0 =A0 =A0 =A0 from test.rb:3
    >
    > Please let me know why is this error coming?


    Kernel#gets tries to read from the file specified by the command line=20
    arguments if there are any. This is documented (and in some cases useful).
    If you don't want this behaviour use IO#gets directly (as in: STDIN.gets).

    HTH,
    Sebastian
    =2D-=20
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Feb 20, 2008
    #2
    1. Advertising

  3. 2008/2/20, Sebastian Hungerecker <>:
    > ycsunil wrote:
    > > C:\Documents and Settings\sunilc\Desktop>ruby test.rb 'add'
    > > input something
    > > test.rb:3:in `gets': No such file or directory - add (Errno::ENOENT)
    > > from test.rb:3
    > >
    > > Please let me know why is this error coming?

    >
    >
    > Kernel#gets tries to read from the file specified by the command line
    > arguments if there are any. This is documented (and in some cases useful).
    > If you don't want this behaviour use IO#gets directly (as in: STDIN.gets).


    Alternatively the script can be fixed like this:

    if (ARGV.shift == 'add')
    puts 'input something'
    gt = gets
    puts gt
    else
    puts 'fail'
    end

    A more complex but also more robust alternative might be to process
    command line options e.g. with OptionParser.

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Feb 20, 2008
    #3
    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. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,706
    Richard Heathfield
    Sep 3, 2003
  2. David
    Replies:
    10
    Views:
    6,078
    Richard Heathfield
    Sep 15, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,716
    Old Wolf
    Jan 20, 2004
  4. =?ISO-8859-1?Q?Thomas_N=FCcker?=

    sys.argv[0] - 'module' object has no attribute 'argv'

    =?ISO-8859-1?Q?Thomas_N=FCcker?=, Jun 30, 2003, in forum: Python
    Replies:
    0
    Views:
    1,002
    =?ISO-8859-1?Q?Thomas_N=FCcker?=
    Jun 30, 2003
  5. RichardOnRails
    Replies:
    8
    Views:
    136
    Robert Klemme
    Feb 2, 2010
Loading...

Share This Page