2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Ruby で楽しいルーレット

Posted at

この記事は?

ターミナル上で気軽にルーレットを楽しめるスクリプトを Ruby で実装します。

2022-05-15 11-26-16.2022-05-15 11_26_57.gif

実装

roulette
#! /usr/bin/env ruby
def clear_line
  print("\e[A\e[K")
end

def blink(item)
  red, yellow = 31, 33
  [red, yellow, red, yellow].each_with_index do |color, i|
    if i > 0
      sleep(0.2)
      clear_line
    end
    puts("\e[#{color}m#{item}\e[0m")
  end
end

# 実行中のスクリプトを Control+C で終了できるようにする。
Signal.trap(:INT) { exit }

items = ARGV
items.cycle.each_with_index do |item, i|
  random_number = rand(items.length)

  puts(item)

  case i
  when 0...20
    sleep(0.1)
  when 20...30
    sleep(0.2)
  when 30...(35 + random_number)
    sleep(0.4)
  else
    sleep(0.4)
    clear_line
    blink(item)
    break
  end

  clear_line
end

実行

$ chmod +x roulette
$ ./roulette むね もも ささみ せせり ぼんじり

参考

2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?