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 668933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:05:44+00:00 2026-05-14T00:05:44+00:00

I have a ruby script that is simultaneously and asynchronously receiving and displaying messages

  • 0

I have a ruby script that is simultaneously and asynchronously receiving and displaying messages from a server, and allowing user input on the console. When a message is received, it is currently being written in the middle of what the user is typing. The input itself isn’t garbled, but it looks horrible. Ideally, it would save the users current input, output the message, and then restore the input on the next line. I’ve done this in c by intercepting every key stroke, but all I remember is that it was a major hassle. I’m fairly new to ruby, so I’m not sure if there is a good way to do this, or how to do it.

Example: User is typing >abcde, and message hello comes in, and user types fgh after. The console would now show:

>abcdehellofgh

and user can continue typing at the end. I would like it to show:

hello
>abcdefgh
  • 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-14T00:05:45+00:00Added an answer on May 14, 2026 at 12:05 am

    The two important parts of my proposal are:
    1. system(“stty raw -echo”) # to instantly get any typed character in combination with
    2. using STDIN.getc with a string-var serving as an input-buffer to reprint the user input.

    Pressing 9 (& boolean variable incoming_message) is used for ultra-simple simulation for your server-messages.
    (Being more realistic would bloat the example without adding much, since you have to adapt the idea for your scenario anyway.)

    Press x for exit.

    Sample output:

    $ ruby simultaneous_input_and_output.rb
    Hello World!9
    The incoming message is: true
    Hello World!9x
    
    You entered:
    Hello World!9x
    End!
    


    #!/usr/bin/ruby
    
    $input_buffer = "";  
    incoming_message = false
    
    begin  
      system("stty raw -echo")  
    
      begin   
        str = STDIN.getc  
        print str.chr  
    
        $input_buffer = $input_buffer + str.chr  
        if str.chr == '9'  
          incoming_message = true  
        end  
        if incoming_message   
          puts "\nThe incoming message is: " + incoming_message.to_s  
          print $input_buffer  
          incoming_message = false  
        end  
      end while (str.chr != 'x')  
    ensure  
      system("stty -raw echo")  
    end  
    
    puts "\n\nYou entered:"   
    puts $input_buffer  
    puts "End!"    
    

    P.S.:
    for system(“stty raw -echo”) see (User Jay)
    How to get a single character without pressing enter?

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

Sidebar

Ask A Question

Stats

  • Questions 410k
  • Answers 410k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer 'OneTwoThree' readStream upToAll: 'Three' May 15, 2026 at 7:24 am
  • Editorial Team
    Editorial Team added an answer Okay I got it. Also the SDK for Air2 has… May 15, 2026 at 7:24 am
  • Editorial Team
    Editorial Team added an answer I think you'll probably have to modify the kernel, but… May 15, 2026 at 7:24 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.