Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6252051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:43:43+00:00 2026-05-24T13:43:43+00:00

By default Ruby opens $stdin and $stdout in buffered mode. This means you can’t

  • 0

By default Ruby opens $stdin and $stdout in buffered mode. This means you can’t use Ruby to perform a grep-like operation filtering text. Is there any way to force Ruby to use line-oriented mode? I’ve seen various solutions including popen3 (which does buffered-mode only) and pty (which doesn’t separately handle $stdout and $stderr, which I require).

How do I do this? Python seems to have the same lack.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:43:44+00:00Added an answer on May 24, 2026 at 1:43 pm

    It looks like your best bet is to use STDOUT.syswrite and STDOUT.sysread – the following seemed to have reasonably good performance, despite being ugly code:

    STDIN.sync = true
    STDOUT.syswrite "Looking for #{ARGV[0]}\n"
    
    def next_line
      mybuff = @overflow || ""
      until mybuff[/\n/]
        mybuff += STDIN.sysread(8)
      end
      overflow = mybuff.split("\n")
      out, *others = overflow
      @overflow = others.join("\n")
      out
    rescue EOFError => e
      false  # NB: There's a bug here, see below
    end
    
    line = next_line
    while line
      STDOUT.syswrite "#{line}\n" if line =~ /#{ARGV[0]}/i
      line = next_line
    end
    

    Note: Not sure you need #sync with #sysread, but if so you should probably sync STDOUT too. Also, it reads 8 bytes at a time into mybuff – you should experiment with this value, it’s highly inefficient / CPU heavy. Lastly, this code is hacky and needs a refactor, but it works – tested it using ls -l ~/* | ruby rgrep.rb doc (where ‘doc’ is the search term)


    Second note: Apparently, I was so busy trying to get it to perform well, I failed to get it to perform correctly! As Dmitry Shevkoplyas has noted, if there is text in @overflow when EOFError is raised, that text will be lost. I believe if you replace the catch with the following, it should fix the problem:

    rescue EOFError => e
      return false unless @overflow && @overflow.length > 0
      output = @overflow
      @overflow = ""
      output
    end
    

    (if you found that helpful, please upvote Dmitry’s answer!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get colors with Ruby's default Curses library? So something like: puts
The default in Ruby on Rails is to have this set to false (in
I have a new Ruby on Rails installation where I can see the default
I'm use Ruby 1.9.2 via RVM. Default version of ruby in system is 1.8.7.
We use the default ruby logging module to log the errors. We use the
In Ruby on Rails, is it possible to change a default action for a
I want to change the default arguments passed to a Ruby function. For example,
I'm building Ruby on Rails 2.3.5 app. By default, Ruby on Rails doesn't provide
By default IntelliJ IDEA 7.0.4 seems to use 4 spaces for indentation in XML
The default rails XML builder escapes all HTML, so something like: atom_feed do |feed|

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.