Skip to content

Commit

Permalink
Setup SI unit system to load piecemeal. Also important to note that t…
Browse files Browse the repository at this point in the history
…he definitions of prefixes changed slightly because Rational(1, 1e24) is not exactly the same as Rational(1, 10**24)
  • Loading branch information
olbrich committed Nov 30, 2015
1 parent 37bdbe9 commit fb213b0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 43 deletions.
9 changes: 5 additions & 4 deletions lib/ruby_units/unit_definitions/si.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative '../definition_collection'
require_relative 'si_base'
require_relative 'si_prefixes'
require_relative 'si_derived'
require_relative '../unit_system'

RubyUnits::UnitSystem.new('International System of Units', :si)

Dir['./lib/ruby_units/unit_definitions/si/*.rb'].each { |file| require file }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RubyUnits::UnitSystem.new('International System of Units', :si) do
RubyUnits::UnitSystem.registered[:si].extend do
base(:meter) do
aliases %w(m meter meters metre metres)
kind :length
Expand Down
38 changes: 38 additions & 0 deletions lib/ruby_units/unit_definitions/si/si_prefixes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
RubyUnits::UnitSystem.registered[:si].extend do
{
googol: [%w(googol), 10**100],
yobi: [%w(Yi Yobi yobi), 2**80],
zebi: [%w(Zi Zebi zebi), 2**70],
exbi: [%w(Ei Exbi exbi), 2**60],
pebi: [%w(Pi Pebi pebi), 2**50],
tebi: [%w(Ti Tebi tebi), 2**40],
gibi: [%w(Gi Gibi gibi), 2**30],
mebi: [%w(Mi Mebi mebi), 2**20],
kibi: [%w(Ki Kibi kibi), 2**10],
yotta: [%w(Y Yotta yotta), 10**24],
zetta: [%w(Z Zetta zetta), 10**21],
exa: [%w(E Exa exa), 10**18],
peta: [%w(P Peta peta), 10**15],
tera: [%w(T Tera tera), 10**12],
giga: [%w(G Giga giga), 10**9],
mega: [%w(M Mega mega), 10**6],
kilo: [%w(k kilo), 10**3],
hecto: [%w(h Hecto hecto), 10**2],
deca: [%w(da Deca deca deka), 10**1],
deci: [%w(d Deci deci), Rational(1, 10**1)],
centi: [%w(c Centi centi), Rational(1, 10**2)],
milli: [%w(m Milli milli), Rational(1, 10**3)],
micro: [%w(u µ Micro micro mc), Rational(1, 10**6)],
nano: [%w(n Nano nano), Rational(1, 10**9)],
pico: [%w(p Pico pico), Rational(1, 10**12)],
femto: [%w(f Femto femto), Rational(1, 10**15)],
atto: [%w(a Atto atto), Rational(1, 10**18)],
zepto: [%w(z Zepto zepto), Rational(1, 10**21)],
yocto: [%w(y Yocto yocto), Rational(1, 10**24)]
}.each do |name, definition|
prefix(name) do
scalar definition.last
aliases definition.first
end
end
end
38 changes: 0 additions & 38 deletions lib/ruby_units/unit_definitions/si_prefixes.rb

This file was deleted.

4 changes: 4 additions & 0 deletions lib/ruby_units/unit_system.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Dir['./lib/ruby_units/definition/*.rb'].each { |file| require file }

module RubyUnits
# This class manages the collection of unit definitions used.
# Prefixes: units that only define a scalar multiplier (like milli, or dozen)
Expand Down Expand Up @@ -99,6 +101,8 @@ def units
base_units.merge(derived_units)
end

# this method can be used to add additional definitions to a
# unit system after it has been created.
def extend(&block)
instance_eval(&block) if block_given?
end
Expand Down

0 comments on commit fb213b0

Please sign in to comment.