3

I understand about the \n that's automatically at the end of puts and gets, and how to deal with those, but is there a way to keep the display point (the 'cursor position', if you will) from moving to a new line after hitting enter for input with gets ?

e.g.

print 'Hello, my name is '
a = gets.chomp
print ', what's your name?'

would end up looking like

Hello, my name is Jeremiah, what's your name?

jeremiah
  • 49
  • 1
  • 6

2 Answers2

7

You can do this by using the (very poorly documented) getch:

require 'io/console'
require 'io/wait'

loop do
  chars = STDIN.getch
  chars << STDIN.getch while STDIN.ready?       # Process multi-char paste
  break if ["\r", "\n", "\r\n"].include?(chars)
  STDOUT.print chars
end

References:

Related follow-up question:

enter & IOError: byte oriented read for character buffered IO

Community
  • 1
  • 1
Denis de Bernardy
  • 75,850
  • 13
  • 131
  • 154
  • Is this only for Ruby 2.1? I am getting `IOError: byte oriented read for character buffered IO` sometimes and I'm not sure if it's because I'm using Ruby 1.9.3. – Darek Nędza Jan 14 '14 at 08:11
  • I've never tried the functions before 2.0, but they're at least partially around if the 1.9.3 is anything to go by. I don't have the beginning of a clue of how functional they were then, though. – Denis de Bernardy Jan 14 '14 at 08:14
  • You know... a lots... I like this :) But I know few :( – Arup Rakshit Jan 14 '14 at 08:32
  • I have tried this in 1.9.3 & 2.0 and I don't think it this error shows because of version I'm using. Here: http://stackoverflow.com/questions/21456829/enter-ioerror-byte-oriented-read-for-character-buffered-io I have created the topic. If you have something to say, please do so. Thank you. – Darek Nędza Jan 30 '14 at 12:43
0

Perhaps I'm missing something, but 'gets.chomp' works just fine does it not? To do what you want, you have to escape the apostrophe or use double-quotes, and you need to include what the user enters in the string that gets printed:

    print 'Hello, my name is '
    a = gets.chomp
    print "#{a}, what's your name?"

    # => Hello, my name is Jeremiah, what's your name?

Works for me. (Edit: Works in TextMate, not Terminal)

Otherwise, you could just do something like this, but I realise it's not quite what you were asking for:

    puts "Enter name"
    a = gets.chomp
    puts "Hello, my name is #{a}, what's your name?"
Kal
  • 2,098
  • 24
  • 23
  • Using your code, I get the output on two lines, with the name repeated at the beginning of the second one (Ruby 2.1). – Denis de Bernardy Jan 14 '14 at 12:58
  • I was testing it directly in TextMate, and it behaved as you wanted it to. But yeah, if I run it in Terminal I see what you mean. Is there any reason you can't get the user input first, and then just do the print on one line? – Kal Jan 14 '14 at 13:06
  • I played around with the extra parameters of `gets`, and it doesn't seem to be supported. `Ctrl+D` might possibly work, though — I haven't tried *that*. – Denis de Bernardy Jan 14 '14 at 13:08