Problem with command gets

Hi guys, I have a simple question. I tried recently to fix a small graphic bug with my command line program I am coding. After I ask a question to the user, the program is waiting for a yes or no from it. It just need to type y | yes | n | no basically.

But how can I avoid gets to make a new line return ? I tried to enable the chomp, but change nothing

IO#gets always reads until it finds a line feed as delimiter. chomp only configures whether that trailing line feed is part of the returned string or not.

If you want to read individual characters, you can try IO#read_char.
Note that you may have to do some extra work there, though.

If you want to build an interactive CLI, I’d recommend using a library for terminal UIs. GitHub - I3oris/reply: A term reader for your REPL is a good choice.

In this particular case, doing gets chomp: true might be enough.

Infortunately no. When I press enter, the program continue to make new line.

This is my code portion:

def getUserAgreement : Bool
            userInput = ""
            userAgreement = false

            loop do
                userInput = gets

                if userInput == ISM::Default::CommandLine::YesReplyOption
                    return true
                if userInput == ISM::Default::CommandLine::NoReplyOption
                    return false

Called like that for example:


userAgreement = Ism.getUserAgreement

Right, your code is doing gets not gets chomp: true

EDIT: Seems true is the default on the argless overload of #gets.

No, I tried with the other solution you gave me, it’s the same result

I just past my code to make sure you can see how I implement (before I tried)

Are you sure? I tried with:

print "With chomp: "

with_chomp = gets chomp: true

p with_chomp

print "Without chomp: "

without_chomp = gets chomp: false

p without_chomp

And output was:

With chomp: foo
Without chomp: bar

If you try the same program does it output the same for you?