Breaking the loop in case of EOF

Discussion in 'Ruby' started by Marcin Górski, Jul 7, 2009.

  1. Hello,
    I need to break a loop when program gets an EOF from STDIN. For example
    in C++ I would do it by following way:

    while(1) {
    if((cin >> a).eof()) break;
    }

    I don't know how do it in Ruby. I tried the idea, but it doesn't work
    well:
    irb(main):007:0> loop do
    irb(main):008:1* str = gets.chomp
    irb(main):009:1> break if STDIN.eof?
    irb(main):010:1> puts "String: " + str
    irb(main):011:1> end
    hey
    joe
    String: hey
    hey
    String: joe
    hey
    String: hey
    => nil

    Thank you for your attention,
    --
    Posted via http://www.ruby-forum.com/.
    Marcin Górski, Jul 7, 2009
    #1
    1. Advertising

  2. Marcin Górski wrote:
    > I need to break a loop when program gets an EOF from STDIN.


    gets will return nil in this case.

    while str = STDIN.gets
    str.chomp!
    puts "String: #{str}"
    end

    Beware that "STDIN.gets" is subtly different from a bare "gets". The
    first always reads from STDIN. The second will read from the files named
    on the command line if the command line is not empty. (See also ARGF).
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Jul 7, 2009
    #2
    1. Advertising

  3. 2009/7/7 Brian Candler <>:
    > Marcin G=F3rski wrote:
    >> I need to break a loop when program gets an EOF from STDIN.

    >
    > gets will return nil in this case.
    >
    > while str =3D STDIN.gets
    > =A0str.chomp!
    > =A0puts "String: #{str}"
    > end
    >
    > Beware that "STDIN.gets" is subtly different from a bare "gets". The
    > first always reads from STDIN. The second will read from the files named
    > on the command line if the command line is not empty. (See also ARGF).


    I believe a more idiomatic way would be

    STDIN.each do |str|
    str.chomp!
    puts "String: #{str}"
    end

    Kind regards

    robert

    --=20
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Jul 7, 2009
    #3
  4. Marcin Górski, Jul 7, 2009
    #4
  5. On Tuesday 07 July 2009 13:23:51 Brian Candler wrote:
    > while str = STDIN.gets
    > str.chomp!
    > puts "String: #{str}"
    > end


    Which can also be written as:
    STDIN.each_line do |str|
    ...
    end
    Sebastian Hungerecker, Jul 7, 2009
    #5
    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. Amy
    Replies:
    0
    Views:
    459
  2. Kobu
    Replies:
    10
    Views:
    605
    Keith Thompson
    Mar 4, 2005
  3. SpreadTooThin

    ifstream eof not reporting eof?

    SpreadTooThin, Jun 13, 2007, in forum: C++
    Replies:
    10
    Views:
    677
    James Kanze
    Jun 15, 2007
  4. Jan Burse
    Replies:
    67
    Views:
    1,029
    Jan Burse
    Mar 14, 2012
  5. Isaac Won
    Replies:
    9
    Views:
    349
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page