Question

Given the following Ruby program:

def getch
  begin
    system("stty raw -echo")
    ch = STDIN.getc
    puts "[#{ch}]"
    ch
  ensure
    system("stty -raw echo")
  end
end

print "Press a key: "
getch
puts "Have a nice day!"

and the following run:

$ ruby a.rb 
Press a key: [t]
              Have a nice day!

Why "Have a nice day!" is indented? Why the output is not like that:

$ ruby a.rb 
Press a key: [t]
Have a nice day!

?

Was it helpful?

Solution

Ok, adding opost should fix it. Change you third line to look like this:

system("stty raw opost -echo")

I hope this is the answer you are looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top