Help us understand the problem. What is going on with this article?

Rubyにて標準出力で表示するプログレスバーなどを作る

More than 3 years have passed since last update.

やりたいのは以下のように更新されていく標準出力を作ること

sample.gif

コツはprint "\r"。これをするとカーソル位置が行頭に戻り、一つの行を上書きしていくことができる。

話はこれで終わりなのだけどこれだけじゃつまらないので、これを使ったお遊びを2つ書きました。

プログレスバーっぽいものを表示する

def progress_bar
  num = rand(20)
  1.upto(num) do |i|
    print "\r#{'=' * i}>"
    sleep 0.2
    puts " End" if i == num
  end
end

progress_bar.gif

おみくじ

def omikuji
  kuji = %w(大吉 中吉 吉 末吉 凶 大凶)
  num = rand(30)
  0.upto(num) do |i|
    print "\r#{kuji.shuffle[i % kuji.size]}"
    sleep 0.1
  end
end

omikuji.gif

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away