Newbie needs help getting user input

Discussion in 'Ruby' started by Peter Vanderhaden, Sep 30, 2007.

  1. I'm trying to learn Ruby and trying to convert a Perl program at the
    same time. I need to prompt a user to enter a number to select a
    processing option. In Perl, I did it this way:

    #
    print STDERR "Enter option: ";
    chomp ($option = <STDIN>);
    #

    I've searched the web and several books, but I haven't found anything
    that works. It also seems that my system doesn't recognize "gets". Can
    anyone help me out, and maybe post a simple input routine I can look at?
    Thanks....
    --
    Posted via http://www.ruby-forum.com/.
     
    Peter Vanderhaden, Sep 30, 2007
    #1
    1. Advertising

  2. Wayne,
    Thanks for the answer. The only problem is that I'm using Windows, not
    Unix. I'm having problems with gems in Windows.
    PV

    Wayne E. Seguin wrote:
    > Peter,
    > $ gem install highline
    >
    >
    >> require "highline"
    >> option = Highline.new.ask("Enter option: ")

    >
    > There are many more things you can do with this gem, check out HighLine:
    > http://highline.rubyforge.org/
    >
    >
    > I hope this helps,
    >
    > ~Wayne


    --
    Posted via http://www.ruby-forum.com/.
     
    Peter Vanderhaden, Sep 30, 2007
    #2
    1. Advertising

  3. Peter Vanderhaden

    7stud -- Guest

    Peter Vanderhaden wrote:
    > I'm trying to learn Ruby and trying to convert a Perl program at the
    > same time. I need to prompt a user to enter a number to select a
    > processing option. In Perl, I did it this way:
    >
    > #
    > print STDERR "Enter option: ";
    > chomp ($option = <STDIN>);
    > #
    >


    print "Enter option (1, 2, 3): "
    input = gets.chomp

    if input == '1'
    puts "I'm executing option 1."
    elsif input == '2'
    puts "I'm executing option 2."
    elsif input == '3'
    puts "I'm executing option 3."
    else
    puts "Bad input."
    end


    Or:

    print "Enter option (1, 2, 3): "
    input = gets.chomp

    case input
    when '1'
    puts "I'm executing option 1."
    when '2'
    puts "I'm executing option 2."
    when '3'
    puts "I'm executing option 3."
    else
    puts "Bad input."
    end

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Sep 30, 2007
    #3
  4. Peter Vanderhaden

    John Joyce Guest

    On Sep 29, 2007, at 8:28 PM, Peter Vanderhaden wrote:

    > I'm trying to learn Ruby and trying to convert a Perl program at the
    > same time. I need to prompt a user to enter a number to select a
    > processing option. In Perl, I did it this way:
    >
    > #
    > print STDERR "Enter option: ";
    > chomp ($option = <STDIN>);
    > #
    >
    > I've searched the web and several books, but I haven't found anything
    > that works. It also seems that my system doesn't recognize
    > "gets". Can
    > anyone help me out, and maybe post a simple input routine I can
    > look at?
    > Thanks....
    > --
    > Posted via http://www.ruby-forum.com/.
    >

    The usual basic thing is:

    puts "Enter option:"
    option = gets.chomp


    If your system isn't recognizing gets, then you've got some other
    problem that should be fixed before writing much code.
     
    John Joyce, Sep 30, 2007
    #4
  5. Peter Vanderhaden wrote:
    > print STDERR "Enter option: ";


    The ruby equivalent of this would be STDERR.print "Enter option: " but why
    STDERR? That hardly looks like an error message to me.

    > chomp ($option = <STDIN>);


    That would be gets.chomp or STDIN.gets.chomp if you want to make sure that
    you read from STDIN.

    > I've searched the web and several books, but I haven't found anything
    > that works. It also seems that my system doesn't recognize "gets".


    Your system is not supposed to recognize gets. If your ruby does not recognize
    gets, there is something seriously wrong with your ruby installation.


    HTH,
    Sebastian
    --
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Sep 30, 2007
    #5
  6. Sebastian,
    My Ruby installation must be corrupt, as it doesn't recognize gets.
    When I try your solution, I get the following error message:

    D:/scripts/ruby/f0.rb:10:in `gets': Bad file descriptor (Errno::EBADF)
    from D:/scripts/ruby/f0.rb:10

    I'd assume the best way to correct this would be to reinstall Ruby?
    Would you agree?

    Thanks,
    PETERV


    Sebastian Hungerecker wrote:

    > Peter Vanderhaden wrote:
    >> print STDERR "Enter option: ";

    >
    > The ruby equivalent of this would be STDERR.print "Enter option: " but
    > why
    > STDERR? That hardly looks like an error message to me.
    >
    >> chomp ($option = <STDIN>);

    >
    > That would be gets.chomp or STDIN.gets.chomp if you want to make
    > sure that
    > you read from STDIN.
    >
    >> I've searched the web and several books, but I haven't found anything
    >> that works. It also seems that my system doesn't recognize "gets".

    >
    > Your system is not supposed to recognize gets. If your ruby does not
    > recognize
    > gets, there is something seriously wrong with your ruby installation.
    >
    >
    > HTH,
    > Sebastian


    --
    Posted via http://www.ruby-forum.com/.
     
    Peter Vanderhaden, Sep 30, 2007
    #6
  7. Sebastian,
    FYI, the reason I'm sending the prompt to STDERR instead of STDOUT is
    that I've used reopen to redirect STDOUT to a file. I did that because
    I want to write data to a file. I'm new to this, so if there's a better
    way to write to the file, I'm all ears :)
    PETERV

    Peter Vanderhaden wrote:
    > Sebastian,
    > My Ruby installation must be corrupt, as it doesn't recognize gets.
    > When I try your solution, I get the following error message:
    >
    > D:/scripts/ruby/f0.rb:10:in `gets': Bad file descriptor (Errno::EBADF)
    > from D:/scripts/ruby/f0.rb:10
    >
    > I'd assume the best way to correct this would be to reinstall Ruby?
    > Would you agree?
    >
    > Thanks,
    > PETERV
    >
    >
    > Sebastian Hungerecker wrote:
    >
    >> Peter Vanderhaden wrote:
    >>> print STDERR "Enter option: ";

    >>
    >> The ruby equivalent of this would be STDERR.print "Enter option: " but
    >> why
    >> STDERR? That hardly looks like an error message to me.
    >>
    >>> chomp ($option = <STDIN>);

    >>
    >> That would be gets.chomp or STDIN.gets.chomp if you want to make
    >> sure that
    >> you read from STDIN.
    >>
    >>> I've searched the web and several books, but I haven't found anything
    >>> that works. It also seems that my system doesn't recognize "gets".

    >>
    >> Your system is not supposed to recognize gets. If your ruby does not
    >> recognize
    >> gets, there is something seriously wrong with your ruby installation.
    >>
    >>
    >> HTH,
    >> Sebastian


    --
    Posted via http://www.ruby-forum.com/.
     
    Peter Vanderhaden, Sep 30, 2007
    #7
  8. Peter Vanderhaden wrote:
    > Sebastian,
    > My Ruby installation must be corrupt, as it doesn't recognize gets.
    > When I try your solution, I get the following error message:
    >
    > D:/scripts/ruby/f0.rb:10:in `gets': Bad file descriptor (Errno::EBADF)
    > from D:/scripts/ruby/f0.rb:10


    Well, he doesn't say that he doesn't recognize gets, he says that an error
    occured while executing gets.
    It should be noted that Kernel#gets will open ARGV[0] as a file and read that
    instead of STDIN when ARGV is not empty, which is the most common cause of
    errors with gets. So if that's the problem in your case, try using STDIN.gets
    instead of Kernel#gets. If that doesn't help, show the code.


    > I'd assume the best way to correct this would be to reinstall Ruby?
    > Would you agree?


    If your gets is really broken and you're not just using it wrong, then yes.


    HTH,
    Sebastian
    --
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Sep 30, 2007
    #8
  9. Peter Vanderhaden wrote:
    > Sebastian,
    > FYI, the reason I'm sending the prompt to STDERR instead of STDOUT is
    > that I've used reopen to redirect STDOUT to a file. I did that because
    > I want to write data to a file. I'm new to this, so if there's a better
    > way to write to the file, I'm all ears :)
    > PETERV


    File.open(filename,'w') do |f|
    f.puts "Text that's supposed to go in the file"
    puts "Text that's supposed to go to the screen"
    f.puts "More text for the file"
    STDERR.puts "Error message"
    end


    HTH,
    Sebastian
    --
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Sep 30, 2007
    #9
  10. Peter Vanderhaden

    7stud -- Guest

    Peter Vanderhaden wrote:
    > Sebastian,
    > FYI, the reason I'm sending the prompt to STDERR instead of STDOUT is
    > that I've used reopen to redirect STDOUT to a file. I did that because
    > I want to write data to a file. I'm new to this, so if there's a better
    > way to write to the file, I'm all ears :)
    >


    How about:

    1)
    f = File.new("data.txt", "w")
    f.write("hello world\n")
    f.close()


    2)
    f = File.open("data.txt", "w") do |file|
    file.puts("goodbye mars")
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Sep 30, 2007
    #10
  11. Peter Vanderhaden

    7stud -- Guest

    7stud -- wrote:
    >
    > 2)
    > f = File.open("data.txt", "w") do |file|
    > file.puts("goodbye mars")
    > end


    Whoops, too slow. And I had some detritus stuck to the front of that
    example anyway. It should be:

    2)
    File.open("data.txt", "w") do |file|
    file.puts("goodbye venus")
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Sep 30, 2007
    #11
  12. 7stud,
    Thanks! This is obviously much better than what I was doing! Being new
    to this type of language, I really appreciate the help! I do have one
    more question for you though. What's the difference between using
    file.puts & file.print?





    7stud -- wrote:
    > 7stud -- wrote:
    >>
    >> 2)
    >> f = File.open("data.txt", "w") do |file|
    >> file.puts("goodbye mars")
    >> end

    >
    > Whoops, too slow. And I had some detritus stuck to the front of that
    > example anyway. It should be:
    >
    > 2)
    > File.open("data.txt", "w") do |file|
    > file.puts("goodbye venus")
    > end


    --
    Posted via http://www.ruby-forum.com/.
     
    Peter Vanderhaden, Sep 30, 2007
    #12
  13. Peter Vanderhaden wrote:
    > What's the difference between using file.puts & file.print?


    puts adds a newline at the end (if the string doesn't already end with a
    newline).

    HTH,
    Sebastian
    --
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Sep 30, 2007
    #13
    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. aspiring geek
    Replies:
    0
    Views:
    319
    aspiring geek
    Dec 7, 2006
  2. Replies:
    3
    Views:
    410
  3. rob
    Replies:
    7
    Views:
    375
    osmium
    Oct 20, 2007
  4. Lee Falataco

    Newbie needs help getting ODBC on ISP setup

    Lee Falataco, May 4, 2004, in forum: ASP General
    Replies:
    3
    Views:
    142
    Steven Burn
    May 4, 2004
  5. Cary Swoveland

    Newbie needs help getting started

    Cary Swoveland, Jan 17, 2010, in forum: Ruby
    Replies:
    11
    Views:
    179
    Phillip Gawlowski
    Jan 19, 2010
Loading...

Share This Page