Why "puts" output is indented in Ruby?

525 views Asked by At

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!

?

1

There are 1 answers

0
indiche On BEST ANSWER

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.