Matthew Lindfield Seager

Matthew Lindfield Seager

Adding an 'in_ticks' method to Numeric in Ruby

Certain fields in Active Directory are stored in “ticks” (1 tick == 100 nanoseconds).

To make using ticks easier in an app I wanted to add an additional time method to Numeric (a lot like Active Support does…

It’s probably not the best way of doing it but in the end I added it using an initialiser:

# 'config/initializers/numeric.rb'
# Be sure to restart your server when you modify this file.

class Numeric
  # Returns the number of ticks (100 nanoseconds) equivalent to the seconds provided.
  # Used with the standard time durations.
  #   2.in_ticks # => 20_000_000
  #   1.hour.in_ticks # => 36_000_000_000
  def in_ticks
    self * 10_000_000