units-system 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +39 -7
- data/Rakefile +0 -1
- data/VERSION +1 -1
- data/lib/units-system.rb +40 -5
- data/test/helper.rb +1 -0
- data/test/test_units-system.rb +3 -0
- data/units-system.gemspec +3 -4
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -1,18 +1,39 @@
|
|
1
1
|
= Ruby Units-System
|
2
2
|
|
3
|
-
Units of measure conversions for Ruby
|
3
|
+
Units of measure conversions for Ruby, using Ruby objects and Ruby syntax rather than text strings.
|
4
4
|
|
5
5
|
There are a number of Ruby units libraries, but I don't think they take this approach (I haven't
|
6
6
|
done much research, though.)
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
There's a couple of caveats for using this module from Ruby 1.8:
|
9
|
+
* To use unit names that start with uppercase letters, the UseBlocks module must be included in the scope
|
10
|
+
(either global scope or a module or class definitions) where the expressions are goign to be used.
|
11
|
+
Alternatively, expressions such as <tt>u{W}</tt> could be replaced by either
|
12
|
+
<tt>u{W()}</tt>, <tt>u{self.W}</tt> (method invocation) or <tt>u{self::W}</tt> (qualified constant).
|
13
|
+
* UTF-8 characters are liberally used in identifiers, so the code must be executed with the <tt>-Ku</tt>
|
14
|
+
option (or setting <tt>$KCODE='UTF8'</tt> before requiring this library.)
|
15
|
+
|
16
|
+
= Usage examples
|
17
|
+
|
18
|
+
== Ruby 1.9
|
19
|
+
|
20
|
+
For use with Ruby 1.9, this gem can be used simply by requiring:
|
21
|
+
|
22
|
+
require 'units-system'
|
23
|
+
|
24
|
+
== Ruby 1.8
|
12
25
|
|
13
|
-
|
26
|
+
This library has been designed for Ruby 1.9; when using it under older versions of Ruby there's
|
27
|
+
a couple of precautions to be taken to use it (which can be used with Ruby 1.9 too):
|
28
|
+
|
29
|
+
$KCODE = 'UTF8' # avoid errors when parsing the required library under Ruby 1.8
|
30
|
+
require 'units-system'
|
31
|
+
include Units::UseBlocks # allow access to capitalized unit names from units/u blocks
|
14
32
|
|
15
|
-
|
33
|
+
Depending on you installation you may have to "require 'rubygems' first.
|
34
|
+
|
35
|
+
The following examples use UTF-8 code; so they can should be used with a "encoding: utf-8" comment at the
|
36
|
+
top of the file for Ruby 1.9, and/or with the ruby command line "-Ku" option for Ruby 1.8.
|
16
37
|
|
17
38
|
To work with units a +units+ block can be used. Beware: in it +self+ is changed,
|
18
39
|
so outer self methods or instance variables are not accessible, unless usigned to
|
@@ -104,6 +125,17 @@ For short expressions, the abbreviation +Units.u+ can be used instead of +Units.
|
|
104
125
|
x = u{120*km/h}
|
105
126
|
puts x.to(u{mi/h}) # => 74.5645430684801*mi/h
|
106
127
|
|
128
|
+
New units can be defined with +Units.define+
|
129
|
+
|
130
|
+
Units.define :kph, 1, Units.u{km/h}
|
131
|
+
puts Units.u{270*kph.to(m/s)} # => 75.0*m/s
|
132
|
+
|
133
|
+
== Caveat
|
134
|
+
|
135
|
+
Note that Ruby variable definition rules imply that this:
|
136
|
+
m = Units.u{m}
|
137
|
+
Results is a nil value (the outter m assignment defines a local m variable even before executing the block, so
|
138
|
+
the m in the block refers to that, yet-unassigned, variable and not to the meter unit)
|
107
139
|
|
108
140
|
== Note on Patches/Pull Requests
|
109
141
|
|
data/Rakefile
CHANGED
@@ -7,7 +7,6 @@ begin
|
|
7
7
|
gem.name = "units-system"
|
8
8
|
gem.summary = %Q{Arithmetic with units of measure}
|
9
9
|
gem.description = %Q{Experimental unit conversion & arithmetic for Ruby 1.9}
|
10
|
-
gem.required_ruby_version = '>= 1.9.1'
|
11
10
|
gem.email = "jgoizueta@gmail.com"
|
12
11
|
gem.homepage = "http://github.com/jgoizueta/units-system"
|
13
12
|
gem.authors = ["Javier Goizueta"]
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/lib/units-system.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
# Ruby
|
3
|
+
# Ruby Units-System experiments.
|
4
4
|
|
5
5
|
module Units
|
6
|
-
|
6
|
+
|
7
7
|
class Measure
|
8
8
|
|
9
9
|
def initialize(mag=1.0, units={})
|
@@ -349,8 +349,9 @@ module Units
|
|
349
349
|
prefix = prefix.to_s
|
350
350
|
if u[0...prefix.length] == prefix
|
351
351
|
factor = f
|
352
|
-
ud = UNITS[u[prefix.length..-1].to_sym]
|
353
|
-
if ud
|
352
|
+
ud = UNITS[u[prefix.length..-1].to_sym]
|
353
|
+
if ud
|
354
|
+
ud = ud.dup
|
354
355
|
ud.name = "#{name}#{ud.name}"
|
355
356
|
break
|
356
357
|
end
|
@@ -395,11 +396,16 @@ module Units
|
|
395
396
|
equivalence = factor*ud.decomposition if ud.decomposition
|
396
397
|
factor *= ud.factor
|
397
398
|
end
|
399
|
+
# si_unit = (factor==1.0) # to save si_units definitions
|
398
400
|
else
|
399
401
|
# unidad compuesta
|
400
402
|
equivalence = args.shift
|
401
403
|
factor = equivalence.to_si.magnitude
|
402
404
|
si_unit = (factor==1.0) # TODO: tolerance?
|
405
|
+
if equivalence.units.empty?
|
406
|
+
# adimensional compound dimension... (special case)
|
407
|
+
equivalence = nil
|
408
|
+
end
|
403
409
|
end
|
404
410
|
elsif args.first.kind_of?(Numeric)
|
405
411
|
# unidad definida en función de otra
|
@@ -407,8 +413,8 @@ module Units
|
|
407
413
|
factor_units = args.shift
|
408
414
|
u = unit(factor_units)
|
409
415
|
dim = u.dim
|
410
|
-
factor *= u.factor
|
411
416
|
equivalence = factor*u.decomposition if u.decomposition
|
417
|
+
factor *= u.factor
|
412
418
|
bias = args.shift
|
413
419
|
else
|
414
420
|
# unidad simple definida en función de una expressión
|
@@ -546,6 +552,26 @@ module Units
|
|
546
552
|
end
|
547
553
|
|
548
554
|
end
|
555
|
+
|
556
|
+
|
557
|
+
# Constant look-up was different in Ruby before 1.9.1
|
558
|
+
# In older Ruby versions, this must be included in any module or class from which units expressions
|
559
|
+
# are to be used in units or u blocks.
|
560
|
+
module UseBlocks
|
561
|
+
if RUBY_VERSION<"1.9"
|
562
|
+
def self.append_features(target)
|
563
|
+
def target.const_missing(name)
|
564
|
+
begin
|
565
|
+
Units.Measure(name)
|
566
|
+
rescue ArgumentError
|
567
|
+
super
|
568
|
+
end
|
569
|
+
end
|
570
|
+
end
|
571
|
+
end
|
572
|
+
end
|
573
|
+
|
574
|
+
include UseBlocks
|
549
575
|
|
550
576
|
# Units definitions
|
551
577
|
|
@@ -575,6 +601,7 @@ module Units
|
|
575
601
|
define :ft, 'foot', 0.3048, :m
|
576
602
|
# in is a Ruby keyword; to avoid having to use self.in we'll define:
|
577
603
|
define :inch, 'inch', 1, :in
|
604
|
+
define :lb, 'pound', 0.45359237, :kg
|
578
605
|
|
579
606
|
# declare derived quantities without named units
|
580
607
|
si_units :speed, u{m/s}
|
@@ -630,5 +657,13 @@ module Units
|
|
630
657
|
|
631
658
|
# define :kp, 'kilopond', :force, u{kg*g0} # or define pond?
|
632
659
|
define :gf, 'gram-force', :force, u{g*g0} # kilopond kp = kgf
|
660
|
+
define :lbf, 'pound-force', :force, u{lb*g0}
|
661
|
+
|
662
|
+
define :dyn, 'dyne', 10, :µN # u{1*g*cm/s**2}
|
663
|
+
define :galUS, 'U.S. liquid gallon', :volume, u{231*self.in**3}
|
664
|
+
define :galUK, 'Imperial gallon', 4.546092, :l
|
665
|
+
define :hp, 'horsepower', :power, u{550*ft*lbf/s}
|
666
|
+
|
667
|
+
define :psi, 'pounds-force per square inch', :pressure, u{lbf/self.in**2}
|
633
668
|
|
634
669
|
end # Units
|
data/test/helper.rb
CHANGED
data/test/test_units-system.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
class TestUnitsSystem < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include Units::UseBlocks
|
4
6
|
|
5
7
|
should "be possible to define Measures with a units block" do
|
6
8
|
assert_equal Units::Measure, Units.units{m}.class
|
@@ -48,6 +50,7 @@ class TestUnitsSystem < Test::Unit::TestCase
|
|
48
50
|
should "convert compound units correctly" do
|
49
51
|
assert_equal 75, Units.u{(270*km/h).in(m/s)}
|
50
52
|
assert_equal 270, Units.u{(75*m/s).in(km/h)}
|
53
|
+
assert_in_delta Units.u{g*cm/s**2}.magnitude, Units.u{dyn.to(g*cm/s**2)}.magnitude, Float::EPSILON
|
51
54
|
end
|
52
55
|
|
53
56
|
should "add units correctly" do
|
data/units-system.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# Generated by jeweler
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{units-system}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Javier Goizueta"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-02-01}
|
13
13
|
s.description = %q{Experimental unit conversion & arithmetic for Ruby 1.9}
|
14
14
|
s.email = %q{jgoizueta@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -31,7 +31,6 @@ Gem::Specification.new do |s|
|
|
31
31
|
s.homepage = %q{http://github.com/jgoizueta/units-system}
|
32
32
|
s.rdoc_options = ["--charset=UTF-8"]
|
33
33
|
s.require_paths = ["lib"]
|
34
|
-
s.required_ruby_version = Gem::Requirement.new(">= 1.9.1")
|
35
34
|
s.rubygems_version = %q{1.3.5}
|
36
35
|
s.summary = %q{Arithmetic with units of measure}
|
37
36
|
s.test_files = [
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: units-system
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Javier Goizueta
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-02-01 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -55,7 +55,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
55
55
|
requirements:
|
56
56
|
- - ">="
|
57
57
|
- !ruby/object:Gem::Version
|
58
|
-
version:
|
58
|
+
version: "0"
|
59
59
|
version:
|
60
60
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
61
|
requirements:
|