quantify 2.0.0 → 2.0.1

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/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.0
1
+ 2.0.1
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  include Quantify
2
3
 
3
4
  # Configure known dimensions, prefixes and units.
@@ -1,4 +1,4 @@
1
-
1
+ # encoding: UTF-8
2
2
  class String
3
3
 
4
4
  def with_superscript_characters
@@ -265,7 +265,7 @@ module Quantify
265
265
  # Dimensions.length.units :symbol #=> [ 'm', 'ft', 'yd', ... ]
266
266
  #
267
267
  def units(by=nil)
268
- Unit.units.select { |unit| unit.dimensions == self }.map(&by)
268
+ Unit.units.select { |unit| unit.dimensions == self }.map(&by).to_a
269
269
  end
270
270
 
271
271
  # Returns the SI unit for the physical quantity described by self.
@@ -319,7 +319,7 @@ module Quantify
319
319
  Unit.base_quantity_si_units.select do |unit|
320
320
  unit.measures == dimension.remove_underscores
321
321
  end.first.clone ** index
322
- end.map(&by)
322
+ end.map(&by).to_a
323
323
  end
324
324
 
325
325
  # Compares the base quantities of two Dimensions objects and returns true if
@@ -454,7 +454,11 @@ module Quantify
454
454
  #
455
455
  def base_quantities
456
456
  quantities = self.instance_variables
457
- quantities.delete("@physical_quantity")
457
+ if RUBY_VERSION < "1.9"
458
+ quantities.delete("@physical_quantity")
459
+ else
460
+ quantities.delete(:@physical_quantity)
461
+ end
458
462
  return quantities
459
463
  end
460
464
 
@@ -475,7 +479,7 @@ module Quantify
475
479
  def to_hash
476
480
  hash = {}
477
481
  base_quantities.each do |variable|
478
- hash[variable.gsub("@","").to_sym] = self.instance_variable_get(variable)
482
+ hash[variable.to_s.gsub("@","").to_sym] = self.instance_variable_get(variable)
479
483
  end
480
484
  return hash
481
485
  end
@@ -25,7 +25,7 @@ module Quantify
25
25
  units = Unit.units
26
26
  end
27
27
  return_format = ( $8 ? $8.to_sym : nil )
28
- units.map(&return_format)
28
+ units.map(&return_format).to_a
29
29
  elsif unit = Unit.for(method)
30
30
  return unit
31
31
  else
@@ -55,8 +55,8 @@ module Quantify
55
55
  raise Quantify::Exceptions::QuantityParseError, "Cannot parse string into value and unit"
56
56
  end
57
57
 
58
- def self.configure &block
59
- self.class_eval &block if block
58
+ def self.configure(&block)
59
+ self.class_eval(&block) if block
60
60
  end
61
61
 
62
62
  attr_accessor :value, :unit
@@ -96,6 +96,10 @@ module Quantify
96
96
  "#{@value} #{@unit.send format}"
97
97
  end
98
98
  end
99
+
100
+ def inspect
101
+ to_s
102
+ end
99
103
 
100
104
  # Converts self into a quantity using the unit provided as an argument. The
101
105
  # new unit must represent the same physical quantity, i.e. have the same
@@ -1,4 +1,3 @@
1
-
2
1
  module Quantify
3
2
  module Unit
4
3
  class Base
@@ -60,8 +59,8 @@ module Quantify
60
59
  #
61
60
  # end
62
61
  #
63
- def self.configure &block
64
- class_eval &block if block
62
+ def self.configure(&block)
63
+ class_eval(&block) if block
65
64
  end
66
65
 
67
66
  attr_reader :name, :symbol, :label, :factor, :dimensions
@@ -402,7 +401,7 @@ module Quantify
402
401
  def alternatives(by=nil)
403
402
  @dimensions.units(nil).reject do |unit|
404
403
  unit.is_equivalent_to?(self) || !unit.acts_as_alternative_unit
405
- end.map(&by)
404
+ end.map(&by).to_a
406
405
  end
407
406
 
408
407
  # Returns the SI unit for the same physical quantity which is represented
@@ -449,8 +448,8 @@ module Quantify
449
448
  #
450
449
  def valid_prefixes(by=nil)
451
450
  return [] if is_compound_unit? && has_multiple_base_units?
452
- return Unit::Prefix.si_prefixes.map(&by) if is_si_unit?
453
- return Unit::Prefix.non_si_prefixes.map(&by) if is_non_si_unit?
451
+ return Unit::Prefix.si_prefixes.map(&by).to_a if is_si_unit?
452
+ return Unit::Prefix.non_si_prefixes.map(&by).to_a if is_non_si_unit?
454
453
  end
455
454
 
456
455
  # Multiply two units together. This results in the generation of a compound
@@ -537,7 +536,7 @@ module Quantify
537
536
  def to_hash
538
537
  hash = {}
539
538
  self.instance_variables.each do |var|
540
- symbol = var.gsub("@","").to_sym
539
+ symbol = var.to_s.gsub("@","").to_sym
541
540
  hash[symbol] = send symbol
542
541
  end
543
542
  return hash
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
 
2
3
  module Quantify
3
4
  module Unit
@@ -60,7 +60,7 @@ module Quantify
60
60
  prefixes = Prefix.prefixes
61
61
  end
62
62
  return_format = ( $4 ? $4.to_sym : nil )
63
- prefixes.map(&return_format)
63
+ prefixes.map(&return_format).to_a
64
64
  elsif prefix = self.for(method)
65
65
  return prefix
66
66
  else
@@ -1,4 +1,4 @@
1
- #! usr/bin/ruby
1
+ # encoding: UTF-8
2
2
 
3
3
  module Quantify
4
4
  module Unit
data/quantify.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{quantify}
8
- s.version = "2.0.0"
8
+ s.version = "2.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andrew Berkeley"]
12
- s.date = %q{2011-09-28}
12
+ s.date = %q{2011-10-18}
13
13
  s.description = %q{A gem to support physical quantities and unit conversions}
14
14
  s.email = %q{andrew.berkeley.is@googlemail.com}
15
15
  s.extra_rdoc_files = [
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  require 'quantify'
2
3
  include Quantify
3
4
 
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  require 'quantify'
2
3
  include Quantify
3
4
 
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  require 'quantify'
2
3
  include Quantify
3
4
 
@@ -104,7 +105,9 @@ describe Quantity do
104
105
  end
105
106
 
106
107
  it "should subtract quantities correctly with different units of same dimension" do
107
- (125.4.kelvin - -211.85.degree_celsius).to_s.should == "64.1 K"
108
+ result = (125.4.kelvin - -211.85.degree_celsius)
109
+ result.value.should be_within(0.00000001).of(64.1)
110
+ result.unit.symbol.should == "K"
108
111
  end
109
112
 
110
113
  it "should subtract quantities correctly with different units of same dimension" do
@@ -202,7 +205,8 @@ describe Quantity do
202
205
 
203
206
  it "should convert compound units to SI correctly" do
204
207
  speed = Quantity.new 100, (Unit.km/Unit.h)
205
- speed.to_si.to_s(:name).should == "27.7777777777778 metres per second"
208
+ speed.to_si.value.should be_within(0.0000000000001).of(27.7777777777778)
209
+ speed.to_si.unit.name.should == "metre per second"
206
210
  end
207
211
 
208
212
  it "should convert compound units to SI correctly" do
@@ -257,14 +261,16 @@ describe Quantity do
257
261
  quantity.to_s.should eql "432.0 yd ft"
258
262
  new_quantity=quantity.rationalize_units
259
263
  quantity.to_s.should eql "432.0 yd ft"
260
- new_quantity.to_s.should eql "144.0 yd²"
264
+ new_quantity.value.should be_within(0.0000001).of(144)
265
+ new_quantity.unit.symbol.should eql "yd²"
261
266
  end
262
267
 
263
268
  it "should rationalize units and modify value in place" do
264
269
  quantity = 12.yards*36.feet
265
270
  quantity.to_s.should eql "432.0 yd ft"
266
271
  quantity.rationalize_units!
267
- quantity.to_s.should eql "144.0 yd²"
272
+ quantity.value.should be_within(0.0000001).of(144)
273
+ quantity.unit.symbol.should eql "yd²"
268
274
  end
269
275
 
270
276
  it "should be greater than" do
data/spec/string_spec.rb CHANGED
@@ -1,4 +1,6 @@
1
- $KCODE = 'UTF8'
1
+ # encoding: UTF-8
2
+ $KCODE = 'UTF8' if RUBY_VERSION < "1.9"
3
+
2
4
  require 'quantify'
3
5
 
4
6
  describe String do
data/spec/unit_spec.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  require 'quantify'
2
3
  include Quantify
3
4
 
@@ -1027,7 +1028,8 @@ describe Unit do
1027
1028
  end
1028
1029
 
1029
1030
  it "should return correct unit ratio quantity with symbols" do
1030
- Unit.ratio(:lb,:kg).to_s(:name).should == "2.20462262184878 pounds per kilogram"
1031
+ Unit.ratio(:lb,:kg).to_s(:name).split(" ").first.to_f.should be_within(0.00000000000001).of(2.20462262184878)
1032
+ Unit.ratio(:lb,:kg).to_s(:name).split(" ")[1..3].join(" ").should == "pounds per kilogram"
1031
1033
  end
1032
1034
 
1033
1035
  it "should return correct unit ratio quantity with symbols and rounding" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quantify
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
8
  - 0
9
- - 0
10
- version: 2.0.0
9
+ - 1
10
+ version: 2.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Berkeley
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-28 00:00:00 +01:00
18
+ date: 2011-10-18 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency