Highline giving EOFERROR, input stream exhausted

Discussion in 'Ruby' started by R.. Kumar, Jun 25, 2010.

  1. R.. Kumar

    R.. Kumar Guest

    I am first taking multiline input from user. User may end input using
    Ctr-d.

    Then I use highline's menu and get:
    /opt/local/lib/ruby1.9/gems/1.9.1/gems/highline-1.5.2/lib/highline.rb:603:in
    `get_line': The input stream is exhausted. (EOFError)

    However, experimentally, if I end multiline input on user entering
    "bye", then highline works, but with Ctrl-D it bombs.

    How can i clear EOF prior to calling highline. I tried:

    HighLine.track_eof = false

    but it pushes the error further (split on nil).

    I've tried reopening stdin with a $stdin.reopen '/dev/tty' but it makes
    no difference.

    My earlier code is:

    def get_lines
    lines = nil
    #$stdin.flush
    $stdin.each_line do |line|
    line.chomp!
    if line =~ /^bye$/
    break
    end
    if lines
    lines << "\n" + line
    else
    lines = line
    end
    end
    return lines
    end

    After calling get_lines, I call this method:

    def _choice prompt, choices
    #HighLine.track_eof = false
    puts "got: #{prompt} ::: #{choices} "
    #$stdin.flush
    STDIN.reopen '/dev/tty'
    choose do |menu|
    menu.prompt = prompt
    menu.choices(choices) do |n| return n; end
    end
    end
     
    R.. Kumar, Jun 25, 2010
    #1
    1. Advertisements

  2. Interesting. Your code does work for me:

    $ cat multi-read.rb=20
    #!/usr/bin/env ruby -wKU

    require "rubygems"
    require "highline/import"

    def get_lines
    lines =3D nil
    $stdin.each_line do |line|
    line.chomp!
    if line =3D~ /^bye$/
    break
    end
    if lines
    lines << "\n" + line
    else
    lines =3D line
    end
    end
    return lines
    end

    def _choice prompt, choices
    puts "got: #{prompt} ::: #{choices} "
    STDIN.reopen '/dev/tty'
    choose do |menu|
    menu.prompt =3D prompt
    menu.choices(choices) do |n| return n; end
    end
    end

    p get_lines
    _choice "? ", "A choice"

    __END__
    $ ruby multi-read.rb=20
    one
    two
    three
    "one\ntwo\nthree"
    got: ? ::: A choice=20
    1. A choice
    ? 1
    "A choice"

    I used Control-D to stop the multi-line entry there.

    What platform are you on and what version of Ruby are you using?

    James Edward Gray II=
     
    James Edward Gray II, Jun 25, 2010
    #2
    1. Advertisements

  3. R.. Kumar

    R.. Kumar Guest

    I copied your program and ran it:

    $ ruby high.rb
    /opt/local/lib/ruby1.9/1.9.1/pathname.rb:270: warning: `*' interpreted
    as argument prefix
    one
    two
    three
    "one\ntwo\nthree"
    got: ? ::: A choice
    1. A choice
    ?
    /opt/local/lib/ruby1.9/gems/1.9.1/gems/highline-1.5.2/lib/highline.rb:603:in
    `get_line': The input stream is exhausted. (EOFError)
    from
    /opt/local/lib/ruby1.9/gems/1.9.1/gems/highline-1.5.2/lib/highline.rb:624:in
    `get_response'
    from
    /opt/local/lib/ruby1.9/gems/1.9.1/gems/highline-1.5.2/lib/highline.rb:218:in
    `ask'
    from
    /opt/local/lib/ruby1.9/gems/1.9.1/gems/highline-1.5.2/lib/highline.rb:317:in
    `choose'
    from high.rb:25:in `_choice'
    from high.rb:32:in `<main>'


    ruby 1.9.1p376 (2009-12-07 revision 26041) [i386-darwin10]

    MAC OSX Intel Snow Leopard
    Darwin laptop-3.local 10.3.0 Darwin Kernel Version 10.3.0: Fri Feb 26
    11:58:09 PST 2010; root:xnu-1504.3.12~1/RELEASE_I386 i386

    Thanks.
     
    R.. Kumar, Jun 26, 2010
    #3
  4. It looks like this is a difference in Ruby 1.9 that isn't related to =
    HighLine:

    $ cat eof_change.rb=20
    puts "Enter multiple lines:"
    p $stdin.read

    puts "Question?"
    p $stdin.gets

    __END__
    $ ruby -v eof_change.rb=20
    ruby 1.8.6 (2010-02-05 patchlevel 399) [i686-darwin10.2.0]
    Enter multiple lines:
    one
    two
    three
    "one\ntwo\nthree\n"
    Question?
    answer
    "answer\n"
    $ rvm use 1.9.2

    info: Using ruby 1.9.2 preview3
    $ ruby -v eof_change.rb=20
    ruby 1.9.2dev (2010-05-31 revision 28117) [x86_64-darwin10.3.0]
    Enter multiple lines:
    one
    two
    three
    "one\ntwo\nthree\n"
    Question?
    nil

    I can't seem to find a way around it.

    James Edward Gray II
     
    James Edward Gray II, Jun 26, 2010
    #4
  5. R.. Kumar

    R.. Kumar Guest

    Yup.
    I ran your snippet with 1.9.1 and with the stock /usr/bin/ruby (ruby
    1.8.7 (2009-06-08 patchlevel 173) [universal-darwin10.0]) which I never
    use, and I get the same results. 1.9.1 fails, 1.8.7 works :-(

    Do you know who to forward this issue to ? Could you possibly inform the
    correct person -- I am not familiar with the process.

    I have an unrelated question. Some months ago i checked the RVM site and
    was frightened off -- it said DO NOT install rvm using gem, and it gave
    some complicated procedure.
    Did you install the rvm gem ? If not, is there a link for the best
    procedure to install. I am now a bit weary of custom installs, it take
    too much out of me to keep maintaining them. Thanks.
     
    R.. Kumar, Jun 26, 2010
    #5
  6. I've asked if this is an intentional change on the Ruby Core mailing =
    list.
    RVM has instructions for how to install it an it is capable of =
    maintaining itself:

    http://rvm.beginrescueend.com/rvm/install/

    It's pretty painless and I highly recommend it.

    James Edward Gray II
     
    James Edward Gray II, Jun 26, 2010
    #6
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.