سؤال

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!

?

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top