LoginSignup
0
0

More than 5 years have passed since last update.

コンソールでカウントダウン表示

Last updated at Posted at 2018-01-28

コンソールでカウントダウンされる表示の方法。
単にprintでバックスペースのエスケープ文字を出力するのみ。

count.rb
print "1秒"
sleep 1
(2..9).each do |num|
  print "\b\b\b" + num.to_s + "秒"
  sleep 1
end

マルチバイトだとバックスペース文字が複数個必要なのかな?
そのへんまだ調べてない。

改善版

count2.rb
count = (200..500).to_a.sample
(1..count).to_a.reverse.each do |num|
  str = num.to_s + "秒"
  print str
  sleep 1
  print "\b" * str.bytesize
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0