Sha256: d24300ca894760609e35d519f5c1a5defcfda4903a7f5a8ed587f6c5bf2be977

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

#--
# multipliers.rb
#
# Copyright (c) 2005 Thomas Sawyer
#
# Ruby License
#
# This module is free software. You may use, modify, and/or redistribute this
# software under the same terms as Ruby.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.
#
# ==========================================================================
# Revision History ::
# --------------------------------------------------------------------------
#  2005.08.10  trans   * Created, inspired by Rich Kilmer's bytes.rb.
# ==========================================================================
#++

#:title: Multipliers
#
# Adds methods to Numeric to make working with
# magnitudes (kilo, mega, giga, milli, micro, etc.)
# as well as bits and bytes easier.
#
#   1.kilo               #=> 1000
#   1.milli              #=> 0.001
#   1.kibi               #=> 1024
#
module Numeric::Multipliers

  # SI Multipliers
  def deka  ; self * 10 ; end
  def hecto ; self * 100 ; end
  def kilo  ; self * 1000 ; end
  def mega  ; self * 1000000 ; end
  def giga  ; self * 1000000000 ; end
  def tera  ; self * 1000000000000 ; end
  def peta  ; self * 1000000000000000 ; end
  def exa   ; self * 1000000000000000000 ; end

  # The little guys ;-)
  def deci  ; self.to_f / 10 ; end
  def centi ; self.to_f / 100 ; end
  def milli ; self.to_f / 1000 ; end
  def micro ; self.to_f / 1000000 ; end
  def nano  ; self.to_f / 1000000000 ; end
  def pico  ; self.to_f / 1000000000000 ; end
  def femto ; self.to_f / 1000000000000000 ; end
  def atto  ; self.to_f / 1000000000000000000 ; end

  # SI Binary
  def kibi ; self * 1024 ; end
  def mebi ; self * 1024**2 ; end
  def gibi ; self * 1024**3 ; end
  def tebi ; self * 1024**4 ; end
  def pebi ; self * 1024**5 ; end
  def exbi ; self * 1024**6 ; end

end

#--
class Numeric
  include Multipliers
end
#++

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mega-0.3.1 lib/mega/multiplier.rb