alchemist 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ PKG_FILES = %w(Rakefile) +
6
6
 
7
7
  gem_spec = Gem::Specification.new do |gem_spec|
8
8
  gem_spec.name = 'alchemist'
9
- gem_spec.version = '0.0.2'
9
+ gem_spec.version = '0.0.3'
10
10
  gem_spec.summary = 'Conversions... like you\'ve never seen them before!'
11
11
  gem_spec.description = 'Conversions... like you\'ve never seen them before!!'
12
12
  gem_spec.email = 'matt@toastyapps.com'
@@ -1,22 +1,374 @@
1
- require 'alchemist/numeric_conversion.rb'
2
-
3
- class Numeric
4
- def from(type)
5
- NumericConversion.new(self, type)
1
+ module Alchemist
2
+ @@si_units = %w[m meter metre meters metres liter litre litres liters l L farad farads F coulombs C gray grays Gy siemen siemens S mhos mho ohm ohms volt volts V ]
3
+ @@si_units + %w[joule joules J newton newtons N lux lx henry henrys H b B bits bytes bit byte lumen lumens lm candela candelas cd]
4
+ @@si_units + %w[tesla teslas T gauss Gs G gram gramme grams grammes g watt watts W pascal pascals Pa]
5
+ @@si_units + %w[becquerel becquerels Bq curie curies Ci]
6
+ @@conversion_table = {
7
+ :absorbed_radiation_dose => {
8
+ :gray => 1.0, :grays => 1.0, :Gy => 1.0,
9
+ :rad => 1.0*10**-2, :rads => 1.0*10**-2
10
+ },
11
+ :angles => {
12
+ :radian => 1.0, :radians => 1.0,
13
+ :degree => 0.0174532925, :degrees => 0.0174532925,
14
+ :arcminute => 2.90888208333*10**-4, :arcminutes => 2.90888208333*10**-4,
15
+ :arcsecond => 4.848136806*10**-6, :arcseconds => 4.848136806*10**-6,
16
+ :mil => 9.817477*10**-4, :mils => 9.817477*10**-4,
17
+ :revolution => 6.283185, :revolutions => 6.283185,
18
+ :circle => 6.28318531, :circles => 6.28318531,
19
+ :right_angle => 1.57079633, :right_angles => 1.57079633,
20
+ :grad => 0.0157079633, :grade => 0.0157079633, :gradian => 0.0157079633, :gon => 0.0157079633, :grads => 0.0157079633, :grades => 0.0157079633, :gradians => 0.0157079633, :gons => 0.0157079633
21
+ },
22
+ :area => {
23
+ :square_meter => 1.0, :square_meters => 1.0, :square_metre => 1.0, :square_metres => 1.0,
24
+ :acre => 4.046873*10**3, :acres => 4.046873*10**3,
25
+ :are => 1.0*10**2, :ares => 1.0*10**2, :a => 1.0*10**2,
26
+ :barn => 1.0*10**-28, :barns => 1.0*10**-28, :b => 1.0*10**-28,
27
+ :circular_mil => 5.067075*10**-10, :circular_mils => 5.067075*10**-10,
28
+ :hectare => 1.0*10**4, :hectares => 1.0*10**4, :ha => 1.0*10**4,
29
+ :square_foot => 9.290304*10**-2, :square_feet => 9.290304*10**-2,
30
+ :square_inch => 6.4516*10**-4, :square_inches => 6.4516*10**-4,
31
+ :square_mile => 2.589988*10**6, :square_miles => 2.589988*10**6,
32
+ :square_yard => 8.361274*10**-1, :square_yards => 8.361274*10**-1
33
+ },
34
+ :capacitance => {
35
+ :farad => 1.0, :farads => 1.0, :F => 1.0,
36
+ :abfarad => 1.0*10**9, :emu_of_capacitance => 1.0*10**9, :abfarads => 1.0*10**9, :emus_of_capacitance => 1.0*10**9,
37
+ :statfarad => 1.112650*10**-12, :esu_of_capacitance => 1.112650*10**-12, :statfarads => 1.112650*10**-12, :esus_of_capacitance => 1.112650*10**-12
38
+ },
39
+ :distance => {
40
+ :meter => 1.0, :metres => 1.0, :meters => 1.0, :m => 1.0,
41
+ :fermi => 1.0*10**-15, :fermis => 1.0*10**-15,
42
+ :micron => 1.0*10**-6, :microns => 1.0*10**-6,
43
+ :chain => 20.1168, :chains => 20.1168,
44
+ :inch => 25.4*10**-3, :inches => 25.4*10**-3, :in => 25.4*10**-3,
45
+ :microinch => 2.54*10**-8, :microinches => 2.54*10**-8,
46
+ :mil => 2.54*10**-05, :mils => 2.54*10**-05,
47
+ :rod => 5.029210, :rods => 5.029210,
48
+ :league => 5556, :leagues => 5556,
49
+ :foot => 0.3048, :feet => 0.3048, :ft => 0.3048,
50
+ :yard => 0.9144, :yards => 0.9144, :yd => 0.9144,
51
+ :mile =>1609.344, :miles =>1609.344, :mi => 1609.344,
52
+ :astronomical_unit => 149.60*10**9, :astronomical_units => 149.60*10**9, :au => 149.60*10**9, :ua => 149.60*10**9,
53
+ :light_year => 9.461*10**15, :light_years => 9.461*10**15, :ly => 9.461*10**15,
54
+ :parsec => 30.857*10**15, :parsecs => 30.857*10**15,
55
+ :nautical_mile => 1852.0, :nautical_miles => 1852.0,
56
+ :admirality_mile => 185.3184, :admirality_miles => 185.3184,
57
+ :fathom => 1.8288, :fathoms => 1.8288,
58
+ :cable_length => 185.2, :cable_lengths => 185.2,
59
+ :angstrom => 100.0*10**-12, :angstroms => 100.0*10**-12,
60
+ :pica => 4.233333*10**-3, :picas => 4.233333*10**-3,
61
+ :printer_pica => 4.217518*10**-3, :printer_picas => 4.217518*10**-3,
62
+ :point => 3.527778*10**-4, :points => 3.527778*10**-4,
63
+ :printer_point => 3.514598*10**-4, :printer_points => 3.514598*10**-4
64
+ },
65
+ :dose_equivalent => {
66
+ :sievert => 1.0, :sieverts => 1.0, :Si => 1.0,
67
+ :rem => 1.0*10**-2, :rems => 1.0*10**-2
68
+ },
69
+ :electric_charge => {
70
+ :coulomb => 1.0, :coulombs => 1.0, :C => 1.0,
71
+ :abcoulomb => 10.0, :abcoulombs => 10.0,
72
+ :ampere_hour => 3.6*10**3, :ampere_hours => 3.6*10**3,
73
+ :faraday => 9.648534*10**4, :faradays => 9.648534*10**4,
74
+ :franklin => 3.335641*10**-10, :franklins => 3.335641*10**-10, :Fr => 3.335641*10**-10,
75
+ :statcoulomb => 3.335641*10**-10, :statcoulombs => 3.335641*10**-10
76
+ },
77
+ :electric_conductance => {
78
+ :siemen => 1.0, :siemens => 1.0, :S => 1.0, :mho => 1.0,
79
+ :abmho => 1.0*10**9, :absiemen => 1.0*10**9, :absiemens => 1.0*10**9,
80
+ :statmho => 1.112650*10**-12, :statsiemen => 1.112650*10**-12, :statsiemens => 1.112650*10**-12
81
+ },
82
+ :electrical_impedance => {
83
+ :ohm => 1.0, :ohms => 1.0,
84
+ :abohm => 1.0*10**-9, :emu_of_resistance => 1.0*10**-9, :abohms => 1.0*10**-9, :emus_of_resistance => 1.0*10**-9,
85
+ :statohm => 8.987552*10**11, :esu_of_resistance => 8.987552*10**11, :statohms => 8.987552*10**11, :esus_of_resistance => 8.987552*10**11
86
+ },
87
+ :electromotive_force => {
88
+ :volt => 1.0, :volts => 1.0, :V => 1.0,
89
+ :abvolt => 1.0*10**-8, :emu_of_electric_potential => 1.0*10**-8, :abvolts => 1.0*10**-8, :emus_of_electric_potential => 1.0*10**-8,
90
+ :statvolt => 2.997925*10**2, :esu_of_electric_potential => 2.997925*10**2, :statvolts => 2.997925*10**2, :esus_of_electric_potential => 2.997925*10**2
91
+ },
92
+ :energy => {
93
+ :joule => 1.0, :joules => 1.0, :J => 1.0, :watt_second => 1.0, :watt_seconds => 1.0,
94
+ :watt_hour => 3.6*10**3, :watt_hours => 3.6*10**3,
95
+ :ton_of_tnt => 4.184*10**9, :tons_of_tnt => 4.184*10**9,
96
+ :therm => 1.05506*10**8, :therms => 1.05506*10**8,
97
+ :us_therm => 1.054804*10**8, :us_therms => 1.054804*10**8,
98
+ :kilowatt_hour => 3.6*10**6, :kilowatt_hours => 3.6*10**6,
99
+ :kilocalorie => 4184.0, :kilocalories => 4184.0,
100
+ :calorie => 4.184, :calories => 4.184,
101
+ :mean_kilocalorie => 4190, :mean_kilocalories => 4190,
102
+ :mean_calorie => 4.190, :mean_calories => 4.190,
103
+ :it_kilocalorie => 4186.8, :it_kilocalories => 4186.8,
104
+ :it_calorie => 4.1868, :it_calories => 4.1868,
105
+ :foot_poundal => 4.214011*10**-2, :foot_poundals => 4.214011*10**-2,
106
+ :foot_pound_force => 1.355818,
107
+ :erg => 1.0*10**-7, :ergs => 1.0*10**-7,
108
+ :electronvolt => 1.602176*10**-19, :electronvolts => 1.602176*10**-19, :eV => 1.602176*10**-19,
109
+ :british_thermal_unit => 1.054350*10**3, :british_thermal_units => 1.054350*10**3,
110
+ :mean_british_thermal_unit => 1.05587*10**3, :mean_british_thermal_units => 1.05587*10**3,
111
+ :it_british_thermal_unit => 1.055056*10**3, :it_british_thermal_units => 1.055056*10**3
112
+ },
113
+ :force => {
114
+ :newton => 1.0, :newtons => 1.0, :N => 1.0,
115
+ :dyne => 1.0*10**-5, :dynes => 1.0*10**-5, :dyn => 1.0*10**-5,
116
+ :kilogram_force => 9.80665, :kgf => 9.80665, :kilopond => 9.80665, :kiloponds => 9.80665, :kp => 9.80665,
117
+ :kip => 4.448222*10**3, :kips => 4.448222*10**3,
118
+ :ounce_force => 2.780139*10**-1, :ozf => 2.780139*10**-1,
119
+ :poundal => 1.382550*10**-1, :poundals => 1.382550*10**-1,
120
+ :pound_force => 4.448222, :lbf => 4.448222,
121
+ :ton_force => 8.896443*10**3
122
+ },
123
+ :illuminance => {
124
+ :lux => 1.0, :lx => 1.0, :lumens_per_square_metre => 1.0, :lumens_per_square_meter => 1.0, :lumen_per_square_metre => 1.0, :lumen_per_square_meter => 1.0,
125
+ :phot => 1.0*10**4, :phots => 1.0*10**4, :ph => 1.0*10**4,
126
+ :lumens_per_square_foot => 10.76391, :footcandle => 10.76391, :lumen_per_square_foot => 10.76391, :footcandles => 10.76391
127
+ },
128
+ :inductance => {
129
+ :henry => 1.0, :henrys => 1.0, :H => 1.0,
130
+ :abhenrys => 1.0*10**-9, :emus_of_inductance => 1.0*10**-9, :abhenry => 1.0*10**-9, :emu_of_inductance => 1.0*10**-9,
131
+ :stathenrys => 8.987552*10**11, :esus_of_inductance => 8.987552*10**11, :stathenry => 8.987552*10**11, :esu_of_inductance => 8.987552*10**11
132
+ },
133
+ :information_storage => {
134
+ :bit => 1.0, :bits => 1.0, :b => 1.0,
135
+ :byte => 8.0, :bytes => 8.0,
136
+ :nibbles => 4.0, :nybbles => 4.0
137
+ },
138
+ :luminous_flux => {
139
+ :lumen => 1.0, :lumens => 1.0, :lm => 1.0
140
+ },
141
+ :luminous_intensity => {
142
+ :candela => 1.0, :candelas => 1.0, :cd => 1.0
143
+ },
144
+ :magnetic_flux => {
145
+ :webers => 1.0, :Wb => 1.0,
146
+ :maxwells => 1.0*10**-8, :Mx => 1.0*10**-8,
147
+ :unit_poles => 1.256637*10**-7
148
+ },
149
+ :magnetic_inductance => {
150
+ :tesla => 1.0, :teslas => 1.0, :T => 1.0,
151
+ :gamma => 1.0*10**-9, :gammas => 1.0*10**-9,
152
+ :gauss => 1.0*10**-4, :Gs => 1.0*10**-4, :G => 1.0*10**-4
153
+ },
154
+ :mass => {
155
+ :gram => 1.0, :gramme => 1.0, :grams => 1.0, :grammes => 1.0, :g => 1.0,
156
+ :carat => 2.0*10**-1, :carats => 2.0*10**-1,
157
+ :ounce => 2.834952*10**1, :ounces => 2.834952*10**1, :oz => 2.834952*10**1,
158
+ :pennyweight => 1.555174, :pennyweights => 1.555174, :dwt => 1.555174,
159
+ :pound => 453.59237, :pounds => 453.59237, :lb => 453.59237, :lbs => 453.59237,
160
+ :troy_pound => 373.2417, :apothecary_pound => 373.2417, :troy_pounds => 373.2417, :apothecary_pounds => 373.2417,
161
+ :slug => 14593.9029, :slugs => 14593.9029,
162
+ :assay_ton => 29.1667, :assay_tons => 29.1667, :AT => 29.1667,
163
+ :metric_ton => 1000000, :metric_tons => 1000000,
164
+ :ton => 907184.74, :tons => 907184.74, :short_tons => 907184.74
165
+ },
166
+ :power => {
167
+ :watt => 1.0, :watts => 1.0, :W => 1.0,
168
+ :british_thermal_unit_per_hour => 2.928751*10**-1, :british_thermal_units_per_hour => 2.928751*10**-1,
169
+ :it_british_thermal_unit_per_hour => 2.930711*10**-1, :it_british_thermal_units_per_hour => 2.930711*10**-1,
170
+ :british_thermal_unit_per_second => 1.054350*10**3, :british_thermal_units_per_second => 1.054350*10**3,
171
+ :it_british_thermal_unit_per_second => 1.055056*10**3, :it_british_thermal_units_per_second => 1.055056*10**3,
172
+ :calorie_per_minute => 6.973333*10**-2, :calories_per_minute => 6.973333*10**-2,
173
+ :calorie_per_second => 4.184, :calories_per_second => 4.184,
174
+ :erg_per_second => 1.0*10**-7, :ergs_per_second => 1.0*10**-7,
175
+ :foot_pound_force_per_hour => 3.766161*10**-4,
176
+ :foot_pound_force_per_minute => 2.259697*10**-2,
177
+ :foot_pound_force_per_second => 1.355818,
178
+ :horsepower => 7.456999*10**2,
179
+ :boiler_horsepower => 9.80950*10**3,
180
+ :electric_horsepower => 7.46*10**2,
181
+ :metric_horsepower => 7.354988*10**2,
182
+ :uk_horsepower => 7.4570*10**2,
183
+ :water_horsepower => 7.46043*10**2,
184
+ :kilocalorie_per_minute => 6.973333*10, :kilocalories_per_minute => 6.973333*10,
185
+ :kilocalorie_per_second => 4.184*10**3, :kilocalories_per_second => 4.184*10**3,
186
+ :ton_of_refrigeration => 3.516853*10**3, :tons_of_refrigeration => 3.516853*10**3
187
+ },
188
+ :pressure => {
189
+ :pascal => 1.0, :pascals => 1.0, :Pa => 1.0,
190
+ :atmosphere => 1.01325*10**5, :atmospheres => 1.01325*10**5,
191
+ :technical_atmosphere => 9.80665*10**4, :technical_atmospheres => 9.80665*10**4,
192
+ :bar => 1.0*10**5, :bars => 1.0*10**5,
193
+ :centimeter_of_mercury => 1.333224*10**3, :centimeters_of_mercury => 1.333224*10**3,
194
+ :centimeter_of_water => 98.0665, :centimeters_of_water => 98.0665, :gram_force_per_square_centimeter => 98.0665,
195
+ :dyne_per_square_centimeter => 1.0*10**-1, :dynes_per_square_centimeter => 1.0*10**-1,
196
+ :foot_of_mercury => 4.063666*10**4, :feet_of_mercury => 4.063666*10**4,
197
+ :foot_of_water => 2.989067*10**3, :feet_of_water => 2.989067*10**3,
198
+ :inch_of_mercury => 3.386389*10**3, :inches_of_mercury => 3.386389*10**3,
199
+ :inch_of_water => 2.490889*10**2, :inches_of_water => 2.490889*10**2,
200
+ :kilogram_force_per_square_centimeter => 9.80665*10**4,
201
+ :kilogram_force_per_square_meter => 9.80665,
202
+ :kilogram_force_per_square_millimeter => 9.80665*10**6,
203
+ :kip_per_square_inch => 6.894757*10**6, :kips_per_square_inch => 6.894757*10**6, :ksi => 6.894757*10**6,
204
+ :millibar => 1.0*10**2, :mbar => 1.0*10**2, :millibars => 1.0*10**2, :mbars => 1.0*10**2,
205
+ :millimeter_of_mercury => 1.333224*10**2, :millimeters_of_mercury => 1.333224*10**2,
206
+ :millimeter_of_water => 9.80665, :millimeters_of_water => 9.80665,
207
+ :poundal_per_square_foot => 1.488164, :poundals_per_square_foot => 1.488164,
208
+ :pound_force_per_square_foot => 47.88026,
209
+ :pound_force_per_square_inch => 6.894757*10**3, :psi => 6.894757*10**3,
210
+ :torr => 1.333224*10**2, :torrs => 1.333224*10**2
211
+ },
212
+ :radioactivity => {
213
+ :becquerel => 1.0, :becquerels => 1.0, :Bq => 1.0,
214
+ :curie => 3.7*10**10, :curies => 3.7*10**10, :Ci => 3.7*10**10
215
+ },
216
+ :temperature => {
217
+ :kelvin => 1.0, :K => 1.0,
218
+ :celsius => [Proc.new{ |t| t + 273.15 }, Proc.new{ |t| t - 273.15 }], :centrigrade => [Proc.new{ |t| t + 273.15 }, Proc.new{ |t| t - 273.15 }],
219
+ :degree_celsius => [Proc.new{ |t| t + 273.15 }, Proc.new{ |t| t - 273.15 }], :degree_centrigrade => [Proc.new{ |t| t + 273.15 }, Proc.new{ |t| t - 273.15 }],
220
+ :degrees_celsius => [Proc.new{ |t| t + 273.15 }, Proc.new{ |t| t - 273.15 }], :degrees_centrigrade => [Proc.new{ |t| t + 273.15 }, Proc.new{ |t| t - 273.15 }],
221
+ :fahrenheit => [Proc.new{ |t| t * (5.0/9.0) + 459.67 }, Proc.new{ |t| t * (9.0/5.0) - 459.67 }],
222
+ :degree_fahrenheit => [Proc.new{ |t| t * (5.0/9.0) + 459.67 }, Proc.new{ |t| t * (9.0/5.0) - 459.67 }],
223
+ :degrees_fahrenheit => [Proc.new{ |t| t * (5.0/9.0) + 459.67 }, Proc.new{ |t| t * (9.0/5.0) - 459.67 }],
224
+ :rankine => 1.8, :rankines => 1.8
225
+ },
226
+ :time => {
227
+ :second => 1.0, :seconds => 1.0, :s => 1.0,
228
+ :minute => 60.0, :minutes => 60.0, :min => 60.0,
229
+ :sidereal_minute => 5.983617, :sidereal_minutes => 5.983617,
230
+ :hour => 3600.0, :hours => 3600.0, :hr => 3600.0,
231
+ :sidereal_hour => 3.590170*10**3, :sidereal_hours => 3.590170*10**3,
232
+ :day => 86400.0, :days => 86400.0,
233
+ :sidereal_day => 8.616409*10**4, :sidereal_days => 8.616409*10**4,
234
+ :shake => 1.0*10**-8, :shakes => 1.0*10**-8,
235
+ :year => 3.1536*10**7, :years => 3.1536*10**7,
236
+ :sidereal_year => 3.155815*10**7, :sidereal_years => 3.155815*10**7,
237
+ :tropical_year => 3.155693*10**7, :tropical_years => 3.155693*10**7
238
+ },
239
+ :volume => {
240
+ :litre => 1.0, :liter => 1.0, :litres => 1.0, :liters => 1.0, :l => 1.0, :L => 1.0,
241
+ :barrel => 1.589873*10**2, :barrels => 1.589873*10**2,
242
+ :bushel => 3.523907*10**1, :bushels => 3.523907*10**1,
243
+ :cubic_meter => 1000.0, :cubic_meters => 1000.0,
244
+ :cup => 2.365882*10**-1, :cups => 2.365882*10**-1,
245
+ :imperial_fluid_ounce => 0.0284130742, :imperial_fluid_ounces => 0.0284130742,
246
+ :ounce => 0.0295735296, :ounces => 0.0295735296, :fluid_ounce => 0.0295735296, :fluid_ounces => 0.0295735296,
247
+ :imperial_gallon => 4.54609, :imperial_gallons => 4.54609,
248
+ :gallon => 3.785412, :gallons => 3.785412,
249
+ :imperial_gill => 1.420653*10**-1, :imperial_gills => 1.420653*10**-1,
250
+ :gill => 1.182941*10**-1, :gills => 1.182941*10**-1, :gi => 1.182941*10**-1,
251
+ :pint => 5.506105*10**-1, :pints => 5.506105*10**-1,
252
+ :liquid_pint => 4.731765*10**-1, :liquid_pints => 4.731765*10**-1,
253
+ :quart => 1.101221, :quarts => 1.101221,
254
+ :liquid_quart => 9.463529*10**-1, :liquid_quarts => 9.463529*10**-1,
255
+ :tablespoon => 0.0147867648, :tablespoons => 0.0147867648,
256
+ :teaspoon => 0.00492892159, :teaspoons => 0.00492892159
257
+ }
258
+ }
259
+ @@british_standard_unit_prefixes = {
260
+ :yotta => 10**24, :Y => 10**24,
261
+ :zetta => 10**21, :Z => 10**21,
262
+ :exa => 10**18, :E => 10**18,
263
+ :peta => 10**15, :P => 10**15,
264
+ :tera => 10**12, :T => 10**12,
265
+ :giga => 10**9, :G => 10**9,
266
+ :mega => 10**6, :M => 10**6,
267
+ :kilo => 10**3, :k => 10**3,
268
+ :hecto => 10**2, :h => 10**2,
269
+ :deca => 10, :da => 10,
270
+ :deci => 10**-1, :d => 10**-1,
271
+ :centi => 10**-2, :c => 10**-2,
272
+ :milli => 10**-3, :m => 10**-3,
273
+ :micro => 10**-6, :u => 10**-6,
274
+ :nano => 10**-9, :n => 10**-9,
275
+ :pico => 10**-12, :p => 10**-12,
276
+ :femto => 10**-15, :f => 10**-15,
277
+ :atto => 10**-18, :a => 10**-18,
278
+ :zepto => 10**-21, :z => 10**-21,
279
+ :yocto => 10**-24, :y => 10**-24
280
+ }
281
+
282
+ def from(unit_name)
283
+ send(unit_name)
284
+ end
285
+
286
+ def self.british_standard_unit_prefixes
287
+ @@british_standard_unit_prefixes
288
+ end
289
+
290
+ def self.conversion_table
291
+ @@conversion_table
292
+ end
293
+
294
+ class NumericConversion
295
+ def to(type = nil)
296
+ unless type
297
+ self
298
+ else
299
+ send(type)
300
+ end
301
+ end
302
+ alias_method :as, :to
303
+
304
+ def base(unit_type)
305
+ if(Alchemist.conversion_table[unit_type][@unit_name].is_a?(Array))
306
+ @exponent * Alchemist.conversion_table[unit_type][@unit_name][0].call(@value)
307
+ else
308
+ @exponent * @value * Alchemist.conversion_table[unit_type][@unit_name]
309
+ end
310
+ end
311
+
312
+ def to_s
313
+ @value.to_s
314
+ end
315
+
316
+ def to_f
317
+ @value
318
+ end
319
+
320
+ private
321
+ def initialize value, unit_name, exponent = 1.0
322
+ @value = value.to_f
323
+ @unit_name = unit_name
324
+ @exponent = exponent
325
+ end
326
+
327
+ def method_missing unit_name, *args, &block
328
+ exponent, unit_name = Alchemist.parse_prefix(unit_name)
329
+ if Conversions[ unit_name ]
330
+ types = Conversions[ @unit_name] & Conversions[ unit_name]
331
+ if types[0] # assume first type
332
+ if(Alchemist.conversion_table[types[0]][unit_name].is_a?(Array))
333
+ Alchemist.conversion_table[types[0]][unit_name][1].call(base(types[0]))
334
+ else
335
+ NumericConversion.new(base(types[0]) / (exponent * Alchemist.conversion_table[types[0]][unit_name]), unit_name)
336
+ end
337
+ else
338
+ raise Exception, "Incompatible Types"
339
+ end
340
+ else
341
+ args.map!{|a| a.is_a?(NumericConversion) ? a.send(@unit_name).to_f / @exponent : a }
342
+ @value = @value.send( unit_name, *args, &block )
343
+ self
344
+ end
345
+ end
6
346
  end
7
347
 
8
- def to(type)
9
- from(type)
348
+ Conversions = {}
349
+ def method_missing unit_name, *args, &block
350
+ exponent, unit_name = Alchemist.parse_prefix(unit_name)
351
+ Conversions[ unit_name ] || super( unit_name, *args, &block )
352
+ NumericConversion.new self, unit_name, exponent
10
353
  end
11
354
 
12
- def method_missing(method,*args, &block)
13
- dummy, unit = NumericConversion.parse_prefix(method)
14
- NumericConversion.conversion_table.each do |type, conversions|
15
- if conversions.keys.include?(unit)
16
- return self.from(method)
17
- end
18
- end
19
- super
355
+ def self.parse_prefix(unit)
356
+ @@british_standard_unit_prefixes.each do |prefix, value|
357
+ if unit.to_s =~ /^#{prefix}.+/ && @@si_units.include?(unit.to_s.gsub(/^#{prefix}/,''))
358
+ return [value, unit.to_s.gsub(/^#{prefix}/,'').to_sym]
359
+ end
360
+ end
361
+ [1.0, unit]
20
362
  end
21
363
 
364
+ @@conversion_table.each do |type, conversions|
365
+ conversions.each do |name, value|
366
+ Conversions[name] ||= []
367
+ Conversions[name] << type
368
+ end
369
+ end
370
+ end
371
+
372
+ class Numeric
373
+ include Alchemist
22
374
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alchemist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Mongeau
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-23 00:00:00 -04:00
12
+ date: 2009-10-24 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,7 +23,6 @@ extra_rdoc_files: []
23
23
 
24
24
  files:
25
25
  - Rakefile
26
- - lib/alchemist/numeric_conversion.rb
27
26
  - lib/alchemist.rb
28
27
  has_rdoc: true
29
28
  homepage: http://github.com/toastyapps/alchemist
@@ -49,7 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
48
  requirements: []
50
49
 
51
50
  rubyforge_project:
52
- rubygems_version: 1.3.4
51
+ rubygems_version: 1.3.5
53
52
  signing_key:
54
53
  specification_version: 3
55
54
  summary: Conversions... like you've never seen them before!
@@ -1,313 +0,0 @@
1
- class NumericConversion < Numeric
2
- @@british_standard_unit_prefixes = {
3
- :yotta => 10**24, :Y => 10**24,
4
- :zetta => 10**21, :Z => 10**21,
5
- :exa => 10**18, :E => 10**18,
6
- :peta => 10**15, :P => 10**15,
7
- :tera => 10**12, :T => 10**12,
8
- :giga => 10**9, :G => 10**9,
9
- :mega => 10**6, :M => 10**6,
10
- :kilo => 10**3, :k => 10**3,
11
- :hecto => 10**2, :h => 10**2,
12
- :deca => 10, :da => 10,
13
- :deci => 10**-1, :d => 10**-1,
14
- :centi => 10**-2, :c => 10**-2,
15
- :milli => 10**-3, :m => 10**-3,
16
- :micro => 10**-6, :u => 10**-6,
17
- :nano => 10**-9, :n => 10**-9,
18
- :pico => 10**-12, :p => 10**-12,
19
- :femto => 10**-15, :f => 10**-15,
20
- :atto => 10**-18, :a => 10**-18,
21
- :zepto => 10**-21, :z => 10**-21,
22
- :yocto => 10**-24, :y => 10**-24
23
- }
24
-
25
- @@british_standard_units = %w[ bits b lumens lm candelas cd newtons N metres meters m seconds s grams grammes g radians farads F henrys H joules J kelvin K ohms watts W litres liters l L coulombs C lux lx siemens S pascals Pa grays Gy webers Wb sieverts Si becquerels Bq]
26
-
27
- @@conversion_table = {
28
- :absorbed_radiation_dose => {
29
- :grays => 1.0, :Gy => 1.0,
30
- :rads => 1.0*10**-2
31
- },
32
- :angles => {
33
- :radians => 1.0,
34
- :degrees => 0.0174532925,
35
- :arcminutes => 2.90888208333*10**-4,
36
- :arcseconds => 4.848136806*10**-6,
37
- :mils => 9.817477*10**-4,
38
- :revolutions => 6.283185,
39
- :circles => 6.28318531,
40
- :right_angles => 1.57079633,
41
- :grads => 0.0157079633, :grades => 0.0157079633, :gradians => 0.0157079633, :gons => 0.0157079633
42
- },
43
- :capacitance => {
44
- :farads => 1.0, :F => 1.0,
45
- :abfarads => 1.0*10**9, :emus_of_capacitance => 1.0*10**9,
46
- :statfarads => 1.112650*10**-12, :esus_of_capacitance => 1.112650*10**-12
47
- },
48
- :distance => {
49
- :metres => 1.0, :meters => 1.0, :m => 1.0,
50
- :fermis => 1.0*10**-15,
51
- :microns => 1.0*10**-6,
52
- :chains => 20.1168,
53
- :inches => 25.4*10**-3, :in => 25.4*10**-3,
54
- :microinches => 2.54*10**-8,
55
- :mils => 2.54*10**-05,
56
- :rods => 5.029210,
57
- :leagues => 5556,
58
- :feet => 0.3048, :ft => 0.3048,
59
- :yards => 0.9144, :yd => 0.9144,
60
- :miles =>1609.344, :mi => 1609.344,
61
- :astronomical_units => 149.60*10**9, :au => 149.60*10**9, :ua => 149.60*10**9,
62
- :light_years => 9.461*10**15, :ly => 9.461*10**15,
63
- :parsecs => 30.857*10**15,
64
- :nautical_miles => 1852.0,
65
- :admirality_miles => 185.3184,
66
- :fathoms => 1.8288,
67
- :cable_lengths => 185.2,
68
- :angstroms => 100.0*10**-12,
69
- :picas => 4.233333*10**-3,
70
- :printer_picas => 4.217518*10**-3,
71
- :points => 3.527778*10**-4,
72
- :printer_points => 3.514598*10**-4
73
- },
74
- :dose_equivalent => {
75
- :sieverts => 1.0, :Si => 1.0,
76
- :rems => 1.0*10**-2
77
- },
78
- :electric_charge => {
79
- :coulombs => 1.0, :C => 1.0,
80
- :abcoulombs => 10.0,
81
- :ampere_hours => 3.6*10**3,
82
- :faradays => 9.648534*10**4,
83
- :franklins => 3.335641*10**-10, :Fr => 3.335641*10**-10,
84
- :statcoulombs => 3.335641*10**-10
85
- },
86
- :electric_conductance => {
87
- :siemens => 1.0, :S => 1.0, :mho => 1.0,
88
- :abmho => 1.0*10**9, :absiemens => 1.0*10**9,
89
- :statmho => 1.112650*10**-12, :statsiemens => 1.112650*10**-12
90
- },
91
- :electrical_impedance => {
92
- :ohms => 1.0,
93
- :abohms => 1.0*10**-9, :emus_of_resistance => 1.0*10**-9,
94
- :statohms => 8.987552*10**11, :esus_of_resistance => 8.987552*10**11
95
- },
96
- :electromotive_force => {
97
- :volts => 1.0, :V => 1.0,
98
- :abvolts => 1.0*10**-8, :emus_of_electric_potential => 1.0*10**-8,
99
- :statvolts => 2.997925*10**2, :esus_of_electric_potential => 2.997925*10**2
100
- },
101
- :energy => {
102
- :joules => 1.0, :J => 1.0, :watt_seconds => 1.0,
103
- :watt_hours => 3.6*10**3,
104
- :tons_of_tnt => 4.184*10**9,
105
- :therms => 1.05506*10**8,
106
- :us_therms => 1.054804*10**8,
107
- :kilowatt_hours => 3.6*10**6,
108
- :kilocalories => 4184.0,
109
- :calories => 4.184,
110
- :mean_kilocalories => 4190,
111
- :mean_calories => 4.190,
112
- :it_kilocalories => 4186.8,
113
- :it_calories => 4.1868,
114
- :foot_poundals => 4.214011*10**-2,
115
- :foot_pound_force => 1.355818,
116
- :ergs => 1.0*10**-7,
117
- :electronvolts => 1.602176*10**-19, :eV => 1.602176*10**-19,
118
- :british_thermal_units => 1.054350*10**3,
119
- :mean_british_thermal_units => 1.05587*10**3,
120
- :it_british_thermal_units => 1.055056*10**3
121
- },
122
- :force => {
123
- :newtons => 1.0, :N => 1.0,
124
- :dynes => 1.0*10**-5, :dyn => 1.0*10**-5,
125
- :kilogram_force => 9.80665, :kgf => 9.80665, :kiloponds => 9.80665, :kp => 9.80665,
126
- :kips => 4.448222*10**3,
127
- :ounce_force => 2.780139*10**-1, :ozf => 2.780139*10**-1,
128
- :poundals => 1.382550*10**-1,
129
- :pound_force => 4.448222, :lbf => 4.448222,
130
- :ton_force => 8.896443*10**3
131
- },
132
- :illuminance => {
133
- :lux => 1.0, :lx => 1.0, :lumens_per_square_metre => 1.0, :lumens_per_square_meter => 1.0,
134
- :phots => 1.0*10**4, :ph => 1.0*10**4,
135
- :lumens_per_square_foot => 10.76391, :footcandle => 10.76391
136
- },
137
- :inductance => {
138
- :henrys => 1.0, :H => 1.0,
139
- :abhenrys => 1.0*10**-9, :emus_of_inductance => 1.0*10**-9,
140
- :stathenrys => 8.987552*10**11, :esus_of_inductance => 8.987552*10**11
141
- },
142
- :information_storage => {
143
- :bits => 1.0, :b => 1.0,
144
- :bytes => 8.0,
145
- :nibbles => 4.0, :nybbles => 4.0
146
- },
147
- :luminous_flux => {
148
- :lumens => 1.0, :lm => 1.0
149
- },
150
- :luminous_intensity => {
151
- :candelas => 1.0, :cd => 1.0
152
- },
153
- :magnetic_flux => {
154
- :webers => 1.0, :Wb => 1.0,
155
- :maxwells => 1.0*10**-8, :Mx => 1.0*10**-8,
156
- :unit_poles => 1.256637*10**-7
157
- },
158
- :magnetic_inductance => {
159
- :teslas => 1.0, :T => 1.0,
160
- :gammas => 1.0*10**-9,
161
- :gauss => 1.0*10**-4, :Gs => 1.0*10**-4, :G => 1.0*10**-4
162
- },
163
- :mass => {
164
- :grams => 1.0, :grammes => 1.0, :g => 1.0,
165
- :carats => 2.0*10**-1,
166
- :ounces => 2.834952*10**1, :oz => 2.834952*10**1,
167
- :pennyweights => 1.555174, :dwt => 1.555174,
168
- :pounds => 453.59237, :lb => 453.59237, :lbs => 453.59237,
169
- :troy_pounds => 373.2417, :apothecary_pounds => 373.2417,
170
- :slugs => 14593.9029,
171
- :assay_tons => 29.1667, :AT => 29.1667,
172
- :metric_tons => 1000000,
173
- :tons => 907184.74, :short_tons => 907184.74
174
- },
175
- :power => {
176
- :watts => 1.0, :W => 1.0,
177
- :british_thermal_units_per_hour => 2.928751*10**-1,
178
- :it_british_thermal_units_per_hour => 2.930711*10**-1,
179
- :british_thermal_units_per_second => 1.054350*10**3,
180
- :it_british_thermal_units_per_second => 1.055056*10**3,
181
- :calories_per_minute => 6.973333*10**-2,
182
- :calories_per_second => 4.184,
183
- :ergs_per_second => 1.0*10**-7,
184
- :foot_pound_force_per_hour => 3.766161*10**-4,
185
- :foot_pound_force_per_minute => 2.259697*10**-2,
186
- :foot_pound_force_per_second => 1.355818,
187
- :horsepower => 7.456999*10**2,
188
- :boiler_horsepower => 9.80950*10**3,
189
- :electric_horsepower => 7.46*10**2,
190
- :metric_horsepower => 7.354988*10**2,
191
- :uk_horsepower => 7.4570*10**2,
192
- :water_horsepower => 7.46043*10**2,
193
- :kilocalorie_per_minute => 6.973333*10,
194
- :kilocalorie_per_second => 4.184*10**3,
195
- :tons_of_refrigeration => 3.516853*10**3
196
- },
197
- :pressure => {
198
- :pascals => 1.0, :Pa => 1.0,
199
- :atmospheres => 1.01325*10**5,
200
- :technical_atmospheres => 9.80665*10**4,
201
- :bars => 1.0*10**5,
202
- :centimeters_of_mercury => 1.333224*10**3,
203
- :centimeters_of_water => 98.0665, :gram_force_per_square_centimeter => 98.0665,
204
- :dynes_per_square_centimeter => 1.0*10**-1,
205
- :feet_of_mercury => 4.063666*10**4,
206
- :feet_of_water => 2.989067*10**3,
207
- :inches_of_mercury => 3.386389*10**3,
208
- :inches_of_water => 2.490889*10**2,
209
- :kilogram_force_per_square_centimeter => 9.80665*10**4,
210
- :kilogram_force_per_square_meter => 9.80665,
211
- :kilogram_force_per_square_millimeter => 9.80665*10**6,
212
- :kips_per_square_inch => 6.894757*10**6, :ksi => 6.894757*10**6,
213
- :millibars => 1.0*10**2, :mbars => 1.0*10**2,
214
- :millimeters_of_mercury => 1.333224*10**2,
215
- :millimeters_of_water => 9.80665,
216
- :poundals_per_square_foot => 1.488164,
217
- :pound_force_per_square_foot => 47.88026,
218
- :pound_force_per_square_inch => 6.894757*10**3, :psi => 6.894757*10**3,
219
- :torrs => 1.333224*10**2
220
- },
221
- :radioactivity => {
222
- :becquerels => 1.0, :Bq => 1.0,
223
- :curies => 3.7*10**10, :Ci => 3.7*10**10
224
- },
225
- :temperature => {
226
- :kelvin => 1.0, :K => 1.0,
227
- :degrees_celsius => [Proc.new{ |t| t + 273.15 }, Proc.new{ |t| t - 273.15 }], :degrees_centrigrade => [Proc.new{ |t| t + 273.15 }, Proc.new{ |t| t - 273.15 }],
228
- :degrees_fahrenheit => [Proc.new{ |t| t * (5.0/9.0) + 459.67 }, Proc.new{ |t| t * (9.0/5.0) - 459.67 }],
229
- :rankines => 1.8
230
- },
231
- :time => {
232
- :seconds => 1.0, :s => 1.0,
233
- :minutes => 60.0, :min => 60.0,
234
- :sidereal_minutes => 5.983617,
235
- :hours => 3600.0, :hr => 3600.0,
236
- :sidereal_hours => 3.590170*10**3,
237
- :days => 86400.0,
238
- :sidereal_days => 8.616409*10**4,
239
- :shakes => 1.0*10**-8,
240
- :years => 3.1536*10**7,
241
- :sidereal_years => 3.155815*10**7,
242
- :tropical_years => 3.155693*10**7
243
- },
244
- :volume => {
245
- :litres => 1.0, :liters => 1.0, :l => 1.0, :L => 1.0,
246
- :barrels => 1.589873*10**2,
247
- :bushels => 3.523907*10**1,
248
- :cubic_meters => 1000.0,
249
- :cups => 2.365882*10**-1,
250
- :imperial_fluid_ounces => 0.0284130742,
251
- :fluid_ounces => 0.0295735296,
252
- :imperial_gallons => 4.54609,
253
- :gallons => 3.785412,
254
- :imperial_gills => 1.420653*10**-1,
255
- :gills => 1.182941*10**-1, :gi => 1.182941*10**-1,
256
- :pints => 5.506105*10**-1,
257
- :liquid_pints => 4.731765*10**-1,
258
- :quarts => 1.101221,
259
- :liquid_quarts => 9.463529*10**-1,
260
- :tablespoons => 0.0147867648,
261
- :teaspoons => 0.00492892159
262
- }
263
- }
264
-
265
-
266
- def initialize(value, from)
267
- @from = from.to_sym
268
- @value = value
269
- end
270
-
271
- def self.conversion_table
272
- @@conversion_table
273
- end
274
-
275
- def per(type)
276
- # todo
277
- end
278
-
279
- def to(to)
280
- to = to.to_sym
281
- f_mult, f_type = self.class.parse_prefix(@from)
282
- t_mult, t_type = self.class.parse_prefix(to)
283
- @@conversion_table.each do |type, conversions|
284
- if conversions.keys.include?(f_type) && conversions.keys.include?(t_type)
285
- if conversions[f_type].is_a?(Array) && conversions[t_type].is_a?(Array)
286
- return t_mult * conversions[t_type][1].call(f_mult * conversions[f_type][0].call(@value))
287
- elsif conversions[f_type].is_a?(Array)
288
- return (f_mult * conversions[f_type][0].call(@value)) / (t_mult * conversions[t_type])
289
- elsif conversions[t_type].is_a?(Array)
290
- return t_mult * conversions[t_type][1].call(f_mult * conversions[f_type] * @value)
291
- else
292
- return @value * (f_mult * conversions[f_type]) / (t_mult * conversions[t_type])
293
- end
294
- end
295
- end
296
- raise Exception, "could not convert types"
297
- end
298
-
299
- def from(to)
300
- temp = @from
301
- @from = to
302
- to(temp)
303
- end
304
-
305
- def self.parse_prefix(unit)
306
- @@british_standard_unit_prefixes.each do |prefix, value|
307
- if unit.to_s =~ /^#{prefix}.+/ && @@british_standard_units.include?(unit.to_s.gsub(/^#{prefix}/,''))
308
- return [value, unit.to_s.gsub(/^#{prefix}/,'').to_sym]
309
- end
310
- end
311
- [1.0, unit]
312
- end
313
- end