Permalink
Browse files

Setup SI unit system to load piecemeal. Also important to note that t…

…he definitions of prefixes changed slightly because Rational(1, 1e24) is not exactly the same as Rational(1, 10**24)
  • Loading branch information...
1 parent 37bdbe9 commit fb213b0d78d42e724f8cf245fe024a1e2bda707d @olbrich committed Nov 30, 2015
View
9 lib/ruby_units/unit_definitions/si.rb
@@ -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 }
View
2 lib/ruby_units/unit_definitions/si_base.rb → ...ruby_units/unit_definitions/si/si_base.rb
@@ -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
View
0 ...ruby_units/unit_definitions/si_derived.rb → ...y_units/unit_definitions/si/si_derived.rb
File renamed without changes.
View
38 lib/ruby_units/unit_definitions/si/si_prefixes.rb
@@ -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
View
38 lib/ruby_units/unit_definitions/si_prefixes.rb
@@ -1,38 +0,0 @@
-RubyUnits::UnitSystem.registered[:si].extend do
- {
- googol: [%w(googol), 1e100],
- 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), 1e24],
- zetta: [%w(Z Zetta zetta), 1e21],
- exa: [%w(E Exa exa), 1e18],
- peta: [%w(P Peta peta), 1e15],
- tera: [%w(T Tera tera), 1e12],
- giga: [%w(G Giga giga), 1e9],
- mega: [%w(M Mega mega), 1e6],
- kilo: [%w(k kilo), 1e3],
- hecto: [%w(h Hecto hecto), 1e2],
- deca: [%w(da Deca deca deka), 1e1],
- deci: [%w(d Deci deci), Rational(1, 1e1)],
- centi: [%w(c Centi centi), Rational(1, 1e2)],
- milli: [%w(m Milli milli), Rational(1, 1e3)],
- micro: [%w(u µ Micro micro mc), Rational(1, 1e6)],
- nano: [%w(n Nano nano), Rational(1, 1e9)],
- pico: [%w(p Pico pico), Rational(1, 1e12)],
- femto: [%w(f Femto femto), Rational(1, 1e15)],
- atto: [%w(a Atto atto), Rational(1, 1e18)],
- zepto: [%w(z Zepto zepto), Rational(1, 1e21)],
- yocto: [%w(y Yocto yocto), Rational(1, 1e24)]
- }.each do |name, definition|
- prefix(name) do
- aliases definition.first
- scalar definition.last
- end
- end
-end
View
4 lib/ruby_units/unit_system.rb
@@ -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)
@@ -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

0 comments on commit fb213b0

Please sign in to comment.