units-system 0.1.1 → 0.2.0

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 CHANGED
@@ -10,22 +10,28 @@ There's a couple of caveats for using this module from Ruby 1.8:
10
10
  (either global scope or a module or class definitions) where the expressions are goign to be used.
11
11
  Alternatively, expressions such as <tt>u{W}</tt> could be replaced by either
12
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>
13
+ Units defined in text form, such as <tt>u('W*h')</tt> will work regardless of whether UseBlocks is
14
+ included or not.
15
+ * UTF-8 characters are liberally used in identifiers, so the code must be executed with the <tt>-Ku</tt>
14
16
  option (or setting <tt>$KCODE='UTF8'</tt> before requiring this library.)
15
-
17
+
16
18
  = Usage examples
17
-
19
+
18
20
  == Ruby 1.9
19
-
21
+
20
22
  For use with Ruby 1.9, this gem can be used simply by requiring:
21
23
 
22
24
  require 'units-system'
23
-
25
+
26
+ For versions other than 1.9.1 this is needed as in Ruby 1.8:
27
+
28
+ include Units::UseBlocks # allow access to capitalized unit names from units/u blocks
29
+
24
30
  == Ruby 1.8
25
31
 
26
32
  This library has been designed for Ruby 1.9; when using it under older versions of Ruby there's
27
33
  a couple of precautions to be taken to use it (which can be used with Ruby 1.9 too):
28
-
34
+
29
35
  $KCODE = 'UTF8' # avoid errors when parsing the required library under Ruby 1.8
30
36
  require 'units-system'
31
37
  include Units::UseBlocks # allow access to capitalized unit names from units/u blocks
@@ -36,7 +42,7 @@ The following examples use UTF-8 code; so they can should be used with a "encodi
36
42
  top of the file for Ruby 1.9, and/or with the ruby command line "-Ku" option for Ruby 1.8.
37
43
 
38
44
  To work with units a +units+ block can be used. Beware: in it +self+ is changed,
39
- so outer self methods or instance variables are not accessible, unless usigned to
45
+ so outer self methods or instance variables are not accessible, unless assigned to
40
46
  local variables.
41
47
 
42
48
  require 'units-system'
@@ -44,7 +50,7 @@ local variables.
44
50
  Units.units do
45
51
 
46
52
  # In the units environment predefined variables are available for all units and they
47
- # can b combined arithmetically:
53
+ # can be combined arithmetically:
48
54
  x = 3*m/s
49
55
  puts x # => 3.0*m/s
50
56
  # Note that SI prefixes (k for kilo, etc.) can be used as part of the unit names:
@@ -56,12 +62,12 @@ local variables.
56
62
  # Let's use some unit powers: convert 3 cubic meters to litres:
57
63
  puts (3*m**3).to(l) # => 3000.0*l
58
64
 
59
- # Now let's convert som imperial units to SI:
65
+ # Now let's convert some imperial units to SI:
60
66
  puts (100*mi/h).to_si # => 44.704*m/s
61
67
 
62
68
  # Note that +in+ is a Ruby keyword, so to use inches you must use +self.in+:
63
69
  puts (10*cm).to(self.in) # => 3.93700787401575*in
64
- # ...or use tha alternative nonstandard name +inch+
70
+ # ...or use the alternative nonstandard name +inch+
65
71
  puts (10*cm).to(inch) # => 3.93700787401575*inch
66
72
 
67
73
  # Now let's use derived units, e.g. power units:
@@ -74,6 +80,7 @@ local variables.
74
80
  puts x.base.abr # => 10000.0 (m^2 kg)/s^3
75
81
 
76
82
  # Note that unit names that start with uppercase letters are OK:
83
+ # (but see the notes on UseBlocks above if this doesn't work)
77
84
  puts 11*W # => 11.0*W
78
85
  puts (2*Mg).to(kg) # => 2000.0*kg
79
86
 
@@ -125,6 +132,17 @@ For short expressions, the abbreviation +Units.u+ can be used instead of +Units.
125
132
  x = u{120*km/h}
126
133
  puts x.to(u{mi/h}) # => 74.5645430684801*mi/h
127
134
 
135
+ Text strings can also be used to define units:
136
+
137
+ puts Units.u('60*km + 10*mi') # => 76.09344*km
138
+ puts Units.u('sin(45*°)') # => 0.707106781186547
139
+ x = Units.u('120*km/h')
140
+ puts x.to(Units.u('mi/h')) # => 74.5645430684801*mi/h
141
+
142
+ And also as the right operand of binary arithmetic operators:
143
+
144
+ puts Units.u('20*km')/'h' # => 20.0*km/h
145
+
128
146
  New units can be defined with +Units.define+
129
147
 
130
148
  Units.define :kph, 1, Units.u{km/h}
@@ -134,7 +152,7 @@ New units can be defined with +Units.define+
134
152
 
135
153
  Note that Ruby variable definition rules imply that this:
136
154
  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
155
+ Results is a nil value (the outer m assignment defines a local m variable even before executing the block, so
138
156
  the m in the block refers to that, yet-unassigned, variable and not to the meter unit)
139
157
 
140
158
  == Note on Patches/Pull Requests
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
data/lib/units-system.rb CHANGED
@@ -10,7 +10,7 @@ module Units
10
10
 
11
11
  # This must be included in any module or class from which units expressions
12
12
  # are to be used in units or u blocks.
13
- # It is not needed in Ruby 1.9.1 due to they way constant loop-up is done in that version,
13
+ # It is not needed in Ruby 1.9.1 due to they way constant look-up is done in that version,
14
14
  # but Ruby 1.9.2 has changed that an requires this again.
15
15
  module UseBlocks
16
16
  def self.append_features(target)
data/lib/units/measure.rb CHANGED
@@ -118,14 +118,16 @@ module Units
118
118
  end
119
119
 
120
120
  def /(other)
121
+ other = Units.units(other) if other.kind_of?(String)
121
122
  self * (other.kind_of?(Numeric) ? 1.0/other : other.inverse)
122
123
  end
123
124
 
124
125
  def inspect
125
- "Measure(#{@magnitude.inspect}, #{@units.inspect})"
126
+ "Units::Measure[#{@magnitude.inspect}, #{@units.inspect}]"
126
127
  end
127
128
 
128
129
  def *(other)
130
+ other = Units.units(other) if other.kind_of?(String)
129
131
  case other
130
132
  when Numeric
131
133
  mag = self.magnitude*other
@@ -147,10 +149,12 @@ module Units
147
149
  end
148
150
 
149
151
  def +(other)
152
+ other = Units.units(other) if other.kind_of?(String)
150
153
  Measure.new self.magnitude+other.to(self.units).magnitude, self.units
151
154
  end
152
155
 
153
156
  def -(other)
157
+ other = Units.units(other) if other.kind_of?(String)
154
158
  self + (-other)
155
159
  end
156
160
 
@@ -177,6 +181,7 @@ module Units
177
181
  end
178
182
 
179
183
  def in(other, mode=:absolute)
184
+ other = Units.units(other) if other.kind_of?(String)
180
185
  other = Measure.new(1.0, other) unless other.kind_of?(Measure)
181
186
  other = other.base
182
187
  this = self.base
@@ -199,6 +204,7 @@ module Units
199
204
  end
200
205
 
201
206
  def to(units, mode=:absolute)
207
+ units = Units.units(units) if units.kind_of?(String)
202
208
  units = units.u if units.kind_of?(Measure)
203
209
  Measure.new self.in(units, mode), units
204
210
  end
@@ -223,7 +229,8 @@ module Units
223
229
  end
224
230
 
225
231
  def coerce(other)
226
- [Measure.new(other, {}), self]
232
+ # other = Units.units(other) if other.kind_of?(String)
233
+ [Measure[other], self]
227
234
  end
228
235
 
229
236
  def u # dimension? unit? only_units? strip_units? units_measure?
@@ -116,4 +116,30 @@ class TestUnitsSystem < Test::Unit::TestCase
116
116
  assert_raise(ArgumentError){Units.u("m"){m}}
117
117
  end
118
118
 
119
+ should "admit units defined as text for conversion arguments" do
120
+ assert_equal 75, Units.u{(270*km/h)}.in('m/s')
121
+ assert_equal 270, Units.u{(75*m/s)}.in('km/h')
122
+ assert_in_delta Units.u{g*cm/s**2}.magnitude, Units.u{dyn}.to('g*cm/s**2').magnitude, Float::EPSILON
123
+ end
124
+
125
+ should "handle well capitalized units names" do
126
+ assert_nothing_raised{Units.units{W}}
127
+ assert_nothing_raised{Units.units{3*W}}
128
+ assert_nothing_raised{Units.units{3*N}}
129
+ assert_nothing_raised{Units.units{3*A}}
130
+ end
131
+
132
+ should "render valid code when inspecting measures" do
133
+ assert_equal Units.u{m}, eval(Units.u{m}.inspect)
134
+ assert_equal Units.u{3*m/s}, eval(Units.u{3*m/s}.inspect)
135
+ assert_equal Units.u{3*m/s+2*km/h}, eval(Units.u{3*m/s+2*km/h}.inspect)
136
+ end
137
+
138
+ should "allow arithmetic between measures and text" do
139
+ assert_equal Units.u{m/s}, Units.u{m}/'s'
140
+ assert_equal Units.u{m*s}, Units.u{m}*'s'
141
+ assert_equal Units.u{m+km}, Units.u{m}+'km'
142
+ assert_equal Units.u{m-km}, Units.u{m}-'km'
143
+ end
144
+
119
145
  end
data/units-system.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "units-system"
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
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"]
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.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -129,7 +129,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
129
129
  version: '0'
130
130
  segments:
131
131
  - 0
132
- hash: -4083810323229241626
132
+ hash: 1858258133218816942
133
133
  required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  none: false
135
135
  requirements: