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

timers gem でタイマーを扱う #ruby

More than 5 years have passed since last update.

概要

timers gem でタイマーを扱います

インストール

$ gem install timers

サンプル

サンプルコード: wait, after

after に設定した秒数が経過すると、ブロックが実行されます。

require 'timers'

timers = Timers::Group.new
timer = timers.after(5)
puts "Start"
timers.wait
puts "Fin"
  • 動作確認
% ruby wait.rb
2015-04-16 21:32:46 +0900
Fin
2015-04-16 21:32:51 +0900

サンプル

サンプルコード: wait_interval, every, fire

every に設定した秒数が経過すると、ブロックが実行されます。

require 'timers'

timers = Timers::Group.new
timer = timers.every(5) { puts "every" }

loop do
  interval = timers.wait_interval
  inputs = gets.chomp
  if inputs.empty?
    timers.fire
  else
    puts inputs
  end
end
  • 動作確認

hoge, hige, 空文字, 空文字 の順に入力

% ruby wait_interval.rb
hoge
hoge
hige
hige


every

every
wait_interval.rb: Interrupt

サンプルコード: pause, resume

pause でタイマーを停止、 resume でタイマーを再開します

require 'timers'

timers = Timers::Group.new
paused_timer = timers.every(5) { puts "fire!!" }

paused_timer.pause
3.times { puts '1:' + Time.now.to_s ;timers.wait }

paused_timer.resume
3.times { puts '2:' + Time.now.to_s;timers.wait }

timers.pause
3.times { puts '3:' + Time.now.to_s;timers.wait }

timers.resume
3.times { puts '4:' + Time.now.to_s;timers.wait }
  • 動作確認
% ruby pause_resume.rb
1:2015-04-16 22:08:31 +0900
1:2015-04-16 22:08:31 +0900
1:2015-04-16 22:08:31 +0900
2:2015-04-16 22:08:31 +0900
fire!!
2:2015-04-16 22:08:36 +0900
fire!!
2:2015-04-16 22:08:41 +0900
fire!!
3:2015-04-16 22:08:46 +0900
3:2015-04-16 22:08:46 +0900
3:2015-04-16 22:08:46 +0900
4:2015-04-16 22:08:46 +0900
fire!!
4:2015-04-16 22:08:51 +0900
fire!!
4:2015-04-16 22:08:56 +0900
fire!!

サンプルコード: delay, cancel

delay で実行の遅延、 cancel でタイマーの中止をします

require 'timers'

timers = Timers::Group.new
paused_timer = timers.every(5) { puts "fire!!" }

timers.pause
3.times { puts '1:' + Time.now.to_s ;timers.wait }

timers.resume
3.times { puts '2:' + Time.now.to_s;timers.wait }

timers.delay(1)
3.times { puts '3:' + Time.now.to_s;timers.wait }

timers.pause
3.times { puts '4:' + Time.now.to_s;timers.wait }

timers.resume
3.times { puts '5:' + Time.now.to_s;timers.wait }

timers.cancel
3.times { puts '6:' + Time.now.to_s;timers.wait }

timers.resume
3.times { puts '7:' + Time.now.to_s;timers.wait }
  • 動作確認
1:2015-04-16 22:20:27 +0900
1:2015-04-16 22:20:27 +0900
1:2015-04-16 22:20:27 +0900
2:2015-04-16 22:20:27 +0900
fire!!
2:2015-04-16 22:20:32 +0900
fire!!
2:2015-04-16 22:20:37 +0900
fire!!
3:2015-04-16 22:20:42 +0900
fire!!
3:2015-04-16 22:20:48 +0900
fire!!
3:2015-04-16 22:20:53 +0900
fire!!
4:2015-04-16 22:20:58 +0900
4:2015-04-16 22:20:58 +0900
4:2015-04-16 22:20:58 +0900
5:2015-04-16 22:20:58 +0900
fire!!
5:2015-04-16 22:21:03 +0900
fire!!
5:2015-04-16 22:21:08 +0900
fire!!
6:2015-04-16 22:21:13 +0900
6:2015-04-16 22:21:13 +0900
6:2015-04-16 22:21:13 +0900
7:2015-04-16 22:21:13 +0900
7:2015-04-16 22:21:13 +0900
7:2015-04-16 22:21:13 +0900

:books: 外部資料

tbpgr
Rubyを扱う人事(研修開発、エンジニア採用) Learning Designer。 tbpgr の読み方は(てぃーびー) 個人ブログでも色々と情報を垂れ流してます。 http://tbpgr.hatenablog.com/
http://tbpgr.hatenablog.com/
studist
「伝えることを、もっと簡単に」をミッションにビジュアルSOPマネジメントプラットフォームのBtoB SaaS「Teachme Biz」を開発・運営するスタートアップ
https://medium.com/studist-dev
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
No 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
ユーザーは見つかりませんでした