Sha256: ee7c6051ae08cd8a2ba642970bd35a357bda7afc63894271aee6872c650c9d37

Contents?: true

Size: 718 Bytes

Versions: 1

Compression:

Stored size: 718 Bytes

Contents

# frozen_string_literal: true

require 'rounding'

# This utility can be used by writing a good luck charm.
# ```rb
#   loop do
#     (@charm ||= GoodLuckCharm.new).tondeke!
#     if @charm.changed_per?(60)
#       puts 'kana' # this statement is called only once in a minute)
#     end
#     sleep 1
#   end
# ````
class GoodLuckCharm
  # 飛んでけ!
  def tondeke!
    @last_time = @current_time
    @current_time = Time.now
    self
  end

  # returns true once a specified period
  # @param [Numeric] seconds 周期
  # @param [Boolean] init returned value at first run
  def changed_per?(seconds, init: false)
    return init unless @last_time && Time.now

    @last_time.ceil_to(seconds) <= Time.now
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
good_luck_charm-0.1.1 lib/good_luck_charm/good_luck_charm.rb