LoginSignup
11
8

More than 5 years have passed since last update.

歌うズンドコキヨシ with Ruby

Last updated at Posted at 2016-03-12

歌うズンドコキヨシ

気づき: キ・ヨ・シ!は歌詞ではなかった

Macのsayコマンドでズンドコ言います。

class RandomZundokoLyrics
  ZUN_DOKO = ['ズン', 'ドコ']

  include Enumerable

  def each
    loop do
      yield ZUN_DOKO.sample
    end
  end
end

class Kiyoshi
  def sing(lyrics)
    lyrics.each do |word|
      system 'say', word
      yield word
    end
  end
end

class ZundokoKiyoshiFan
  CHANT = 'キ・ヨ・シ!'
  CHANT_TIMING_LYRICS = ['ズン', 'ズン', 'ズン', 'ズン', 'ドコ']

  def initialize
    @words_buffer = []
  end

  def <<(word)
    @words_buffer << word

    if @words_buffer.size > CHANT_TIMING_LYRICS.size
      @words_buffer.shift
    end
  end

  def chant_timing?
    @words_buffer == CHANT_TIMING_LYRICS
  end

  def chant
    system 'say', CHANT
  end
end

def ズンドコ♪
  kiyoshi = Kiyoshi.new
  lyrics  = RandomZundokoLyrics.new
  fan     = ZundokoKiyoshiFan.new
  kiyoshi.sing(lyrics) do |word|
    fan << word
    fan.chant and exit if fan.chant_timing?
  end
end

ズンドコ♪

実行例

(sayコマンドから音声が聞こえてくる)

その他のキヨシ

11
8
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
11
8