How to overwrite a printed line in the shell with Ruby?

后端 未结 3 1787
北荒
北荒 2020-12-13 17:23

How would I overwrite the previously printed line in a Unix shell with Ruby?

Say I\'d like to output the current time on a shell every second, but instead of stackin

3条回答
  •  情深已故
    2020-12-13 18:13

    You can use the \r escape sequence at the end of the line (the next line will overwrite this line). Following your example:

    require 'time'
    
    loop do
      time = Time.now.to_s + "\r"
      print time
      $stdout.flush
      sleep 1
    end
    

提交回复
热议问题