alchemist 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ PKG_FILES = %w(Rakefile) +
5
+ Dir.glob("{lib}/**/*")
6
+
7
+ gem_spec = Gem::Specification.new do |gem_spec|
8
+ gem_spec.name = 'alchemist'
9
+ gem_spec.version = '0.0.1'
10
+ gem_spec.summary = 'Conversions... like you\'ve never seen them before!'
11
+ gem_spec.description = 'Conversions... like you\'ve never seen them before!!'
12
+ gem_spec.email = 'matt@toastyapps.com'
13
+ gem_spec.homepage = 'http://github.com/toastyapps/alchemist'
14
+ gem_spec.authors = ["Matthew Mongeau"]
15
+ gem_spec.files = PKG_FILES
16
+ end
17
+
18
+ desc "Generate a gemspec file"
19
+ task :gemspec do
20
+ File.open("#{gem_spec.name}.gemspec", "w") do |f|
21
+ f.write gem_spec.to_yaml
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ require 'alchemist/numeric_conversion.rb'
2
+
3
+ class Numeric
4
+ def from(type)
5
+ NumericConversion.new(self, type)
6
+ end
7
+
8
+ def to(type)
9
+ from(type)
10
+ end
11
+
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
20
+ end
21
+
22
+ end
@@ -0,0 +1,313 @@
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
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alchemist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Mongeau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-23 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Conversions... like you've never seen them before!!
17
+ email: matt@toastyapps.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - Rakefile
26
+ - lib/alchemist/numeric_conversion.rb
27
+ - lib/alchemist.rb
28
+ has_rdoc: true
29
+ homepage: http://github.com/toastyapps/alchemist
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.3.4
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Conversions... like you've never seen them before!
56
+ test_files: []
57
+