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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:24:50+00:00 2026-05-17T00:24:50+00:00

Having looked at this question, I have the following code: $/ = \0 answer

  • 0

Having looked at this question, I have the following code:

$/ = "\0"
answer = STDIN.gets

Now, I was hoping that this would allow the user to:

  • enter a multi-line input, terminating by pressing Ctrl-D.
  • enter a single line input, terminating by pressing Ctrl-D.
  • enter a “nothing” input, terminating by pressing Ctrl-D.

However, the behaviour I actually see is that:

  • The user can enter a multi-line input fine.
  • The user can not enter a single line input, unless they hit Ctrl-D twice.
  • The user can enter a “nothing” input if they hit Ctrl-D straight away.

So, why does the single line situation (i.e. if the user has entered some text but no newline and then hit Ctrl-D) require two presses of Ctrl-D? And why does it work then if the user enters nothing? (I have noted that if they enter nothing and hit Ctrl-D, I don’t get an empty string but the nil class – I discovered this when trying to call .empty? on the result, since it suddenly failed horribly. If there is a way to get it to return an empty string as well, that would be nice. I prefer checking .empty? to ==, and don’t particularly want to define .empty? for the nil class.)

EDIT: Since I really would like to know the “correct way” to do this in Ruby, I am offering a bounty of 200 rep. I will also accept answers that give another way of entering terminal multi-line input with a sensible “submit” procedure – I will be the judge of ‘suitable’. For example, we’re currently using two “\n”s, but that’s not suitable, as it blocks paragraphs and is unintuitive.

  • 1 1 Answer
  • 4 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-17T00:24:51+00:00Added an answer on May 17, 2026 at 12:24 am

    The basic problem is the terminal itself. See many of the related links to the right of your post. To get around this you need to put the terminal in a raw state. The following worked for me on a Solaris machine:

    #!/usr/bin/env ruby
    # store the old stty settings
    old_stty = `stty -g`
    # Set up the terminal in non-canonical mode input processing
    # This causes the terminal to process one character at a time
    system "stty -icanon min 1 time 0 -isig"
    answer = ""
    while true
      char = STDIN.getc
      break if char == ?\C-d # break on Ctrl-d
      answer += char.chr
    end
    system "stty #{old_stty}" # restore stty settings
    answer
    

    I’m not sure if the storing and restoring of the stty settings is necessary but I’ve seen other people do it.

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

Sidebar

Related Questions

Have looked quite hard for this answer but having no luck. I have 3
I have looked everywhere for the answer to this question and while there are
I have looked a lot for the answer to the following question but I
I have looked at a lot of different varriations of this question but i
I've already looked around but couldn't find the exact solution/problem I'm having right now.
Having used storyboards for a while now I have found them extremely useful however,
[I'm not sure if this question has been asked, though I've looked around a
In all the Java source code examples I have looked at the listeners have
Following on from this question MVC3 - Should I design my Model to be
Edit1: I realize this is hard to understand this question without having an insight

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.