Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ language: ruby
rvm:
- 1.9.3
- jruby-19mode # JRuby in 1.9 mode
- 2.0.0
jdk:
- openjdk6
- openjdk7
matrix:
exclude:
- rvm: 1.9.3
jdk: openjdk7
script: bundle exec rake --trace
script: bundle exec rake --trace
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

[![Build Status](https://secure.travis-ci.org/olbrich/ruby-units.png)](http://travis-ci.org/olbrich/ruby-units)

Kevin C. Olbrich, Ph.D.

[Sciwerks.com](http://www.sciwerks.com)
Kevin C. Olbrich, Ph.D.

Project page: [http://github.com/olbrich/ruby-units](http://github.com/olbrich/ruby-units)

Expand Down Expand Up @@ -36,7 +34,7 @@ This package may be installed using: `gem install ruby-units`
2. use SI notation when possible
3. avoid using spaces in unit names

## Unit compatability:
## Unit compatibility:
Many methods require that the units of two operands are compatible. Compatible units are those that can be easily converted into each other, such as 'meters' and 'feet'.

unit1 =~ unit2 #=> true if units are compatible
Expand Down Expand Up @@ -170,4 +168,19 @@ This is useful for changing display names, adding aliases, etc.
Unit.redefine!("cup") do |cup|
cup.display_name = "cup"
end



### Namespaced Class

Sometimes the default class 'Unit' may conflict with other gems or applications. Internally ruby-units defines itself using the RubyUnits namespace.
The actual class of a unit is the RubyUnits::Unit. For simplicity and backwards compatiblity, the '::Unit' class is defined as an alias to '::RubyUnits::Unit'.

To load ruby-units without this alias...

require 'ruby-units/namespaced'

When using bundler...

gem 'ruby-units', require: 'namespaced'

Note: when using the namespaced version, the Unit('unit string') helper will not be defined.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.3
1.4.4
1 change: 1 addition & 0 deletions lib/ruby-units
21 changes: 8 additions & 13 deletions lib/ruby-units.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
$LOAD_PATH << File.dirname(__FILE__)
require "ruby_units/version"
require "ruby_units/definition"
require "ruby_units/cache"
require 'ruby_units/array'
require 'ruby_units/date'
require 'ruby_units/time'
require 'ruby_units/math'
require 'ruby_units/numeric'
require 'ruby_units/object'
require 'ruby_units/string'
require 'ruby_units/unit'
require 'ruby_units/fixnum'
require 'ruby_units/unit_definitions'

require_relative 'ruby_units/namespaced'

# only include the Unit('unit') helper if we aren't fully namespaced
require_relative 'ruby_units/object'


Unit = RubyUnits::Unit
8 changes: 4 additions & 4 deletions lib/ruby_units/array.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Array
# Construct a unit from an array
# @example [1, 'mm'].to_unit => Unit("1 mm")
# @return (see Unit#initialize)
# @example [1, 'mm'].to_unit => RubyUnits::Unit("1 mm")
# @return (see RubyUnits::Unit#initialize)
# @param [Object] other convert to same units as passed
def to_unit(other = nil)
other ? Unit.new(self).convert_to(other) : Unit.new(self)
other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
end
alias :unit :to_unit
alias :u :to_unit
end
end
4 changes: 2 additions & 2 deletions lib/ruby_units/cache.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Unit < Numeric
class RubyUnits::Unit < Numeric
@@cached_units = {}

class Cache
Expand All @@ -17,4 +17,4 @@ def self.clear
end

end
end
end
12 changes: 6 additions & 6 deletions lib/ruby_units/definition.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Unit < Numeric
class RubyUnits::Unit < Numeric

# Handle the definition of units
class Definition
Expand Down Expand Up @@ -36,8 +36,8 @@ def initialize(_name, _definition = [], &block)
@aliases ||= (_definition[0] || [_name])
@scalar ||= _definition[1]
@kind ||= _definition[2]
@numerator ||= _definition[3] || Unit::UNITY_ARRAY
@denominator ||= _definition[4] || Unit::UNITY_ARRAY
@numerator ||= _definition[3] || RubyUnits::Unit::UNITY_ARRAY
@denominator ||= _definition[4] || RubyUnits::Unit::UNITY_ARRAY
@display_name ||= @aliases.first
end

Expand Down Expand Up @@ -90,11 +90,11 @@ def unity?
# units are base units if the scalar is one, and the unit is defined in terms of itself.
# @return [Boolean]
def base?
(self.denominator == Unit::UNITY_ARRAY) &&
(self.numerator != Unit::UNITY_ARRAY) &&
(self.denominator == RubyUnits::Unit::UNITY_ARRAY) &&
(self.numerator != RubyUnits::Unit::UNITY_ARRAY) &&
(self.numerator.size == 1) &&
(self.scalar == 1) &&
(self.numerator.first == self.name)
end
end
end
end
4 changes: 2 additions & 2 deletions lib/ruby_units/fixnum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Fixnum
# @return [Unit, Integer]
def quo_with_units(other)
case other
when Unit
when RubyUnits::Unit
self * other.inverse
else
quo_without_units(other)
Expand All @@ -19,4 +19,4 @@ def quo_with_units(other)
alias / quo_with_units
end
# :nocov_19:
end
end
24 changes: 12 additions & 12 deletions lib/ruby_units/math.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Math
alias :unit_sqrt :sqrt
# @return [Numeric]
def sqrt(n)
if Unit === n
if RubyUnits::Unit === n
(n**(Rational(1,2))).to_unit
else
unit_sqrt(n)
Expand All @@ -23,7 +23,7 @@ def sqrt(n)
alias :unit_cbrt :cbrt
# @return [Numeric]
def cbrt(n)
if Unit === n
if RubyUnits::Unit === n
(n**(Rational(1,3))).to_unit
else
unit_cbrt(n)
Expand All @@ -39,7 +39,7 @@ def cbrt(n)
alias :unit_sin :sin
# @return [Numeric]
def sin(n)
Unit === n ? unit_sin(n.convert_to('radian').scalar) : unit_sin(n)
RubyUnits::Unit === n ? unit_sin(n.convert_to('radian').scalar) : unit_sin(n)
end
# @return [Numeric]
module_function :unit_sin
Expand All @@ -49,7 +49,7 @@ def sin(n)
alias :unit_cos :cos
# @return [Numeric]
def cos(n)
Unit === n ? unit_cos(n.convert_to('radian').scalar) : unit_cos(n)
RubyUnits::Unit === n ? unit_cos(n.convert_to('radian').scalar) : unit_cos(n)
end
# @return [Numeric]
module_function :unit_cos
Expand All @@ -59,7 +59,7 @@ def cos(n)
alias :unit_sinh :sinh
# @return [Numeric]
def sinh(n)
Unit === n ? unit_sinh(n.convert_to('radian').scalar) : unit_sinh(n)
RubyUnits::Unit === n ? unit_sinh(n.convert_to('radian').scalar) : unit_sinh(n)
end
# @return [Numeric]
module_function :unit_sinh
Expand All @@ -69,7 +69,7 @@ def sinh(n)
alias :unit_cosh :cosh
# @return [Numeric]
def cosh(n)
Unit === n ? unit_cosh(n.convert_to('radian').scalar) : unit_cosh(n)
RubyUnits::Unit === n ? unit_cosh(n.convert_to('radian').scalar) : unit_cosh(n)
end
# @return [Numeric]
module_function :unit_cosh
Expand All @@ -79,7 +79,7 @@ def cosh(n)
alias :unit_tan :tan
# @return [Numeric]
def tan(n)
Unit === n ? unit_tan(n.convert_to('radian').scalar) : unit_tan(n)
RubyUnits::Unit === n ? unit_tan(n.convert_to('radian').scalar) : unit_tan(n)
end
# @return [Numeric]
module_function :tan
Expand All @@ -89,7 +89,7 @@ def tan(n)
alias :unit_tanh :tanh
# @return [Numeric]
def tanh(n)
Unit === n ? unit_tanh(n.convert_to('radian').scalar) : unit_tanh(n)
RubyUnits::Unit === n ? unit_tanh(n.convert_to('radian').scalar) : unit_tanh(n)
end
# @return [Numeric]
module_function :unit_tanh
Expand All @@ -100,7 +100,7 @@ def tanh(n)
# Convert parameters to consistent units and perform the function
# @return [Numeric]
def hypot(x,y)
if Unit === x && Unit === y
if RubyUnits::Unit === x && RubyUnits::Unit === y
(x**2 + y**2)**(1/2)
else
unit_hypot(x,y)
Expand All @@ -115,9 +115,9 @@ def hypot(x,y)
# @return [Numeric]
def atan2(x,y)
case
when (x.is_a?(Unit) && y.is_a?(Unit)) && (x !~ y)
raise ArgumentError, "Incompatible Units"
when (x.is_a?(Unit) && y.is_a?(Unit)) && (x =~ y)
when (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && (x !~ y)
raise ArgumentError, "Incompatible RubyUnits::Units"
when (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && (x =~ y)
Math::unit_atan2(x.base_scalar, y.base_scalar)
else
Math::unit_atan2(x,y)
Expand Down
16 changes: 16 additions & 0 deletions lib/ruby_units/namespaced.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

$LOAD_PATH << File.dirname(__FILE__)

# require_relative this file to avoid creating an class alias from Unit to RubyUnits::Unit
require_relative 'version'
require_relative 'definition'
require_relative 'cache'
require_relative 'array'
require_relative 'date'
require_relative 'time'
require_relative 'math'
require_relative 'numeric'
require_relative 'string'
require_relative 'unit'
require_relative 'fixnum'
require_relative 'unit_definitions'
6 changes: 3 additions & 3 deletions lib/ruby_units/numeric.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class Numeric
# make a unitless unit with a given scalar
# @return (see Unit#initialize)
# @return (see RubyUnits::Unit#initialize)
def to_unit(other = nil)
other ? Unit.new(self, other) : Unit.new(self)
other ? RubyUnits::Unit.new(self, other) : RubyUnits::Unit.new(self)
end
alias :unit :to_unit
alias :u :to_unit
end
end
10 changes: 5 additions & 5 deletions lib/ruby_units/string.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'time'
class String
# make a string into a unit
# @return (see Unit#initialize)
# @return (see RubyUnits::Unit#initialize)
def to_unit(other = nil)
other ? Unit.new(self).convert_to(other) : Unit.new(self)
other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
end
alias :unit :to_unit
alias :u :to_unit
Expand All @@ -16,7 +16,7 @@ def to_unit(other = nil)
def %(*args)
return "" if self.empty?
case
when args.first.is_a?(Unit)
when args.first.is_a?(RubyUnits::Unit)
args.first.to_s(self)
when (!defined?(Uncertain).nil? && args.first.is_a?(Uncertain))
args.first.to_s(self)
Expand All @@ -27,8 +27,8 @@ def %(*args)
end
end

# @param (see Unit#convert_to)
# @return (see Unit#convert_to)
# @param (see RubyUnits::Unit#convert_to)
# @return (see RubyUnits::Unit#convert_to)
def convert_to(other)
self.unit.convert_to(other)
end
Expand Down
17 changes: 9 additions & 8 deletions lib/ruby_units/time.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'time'
#
# Time math is handled slightly differently. The difference is considered to be an exact duration if
# the subtracted value is in hours, minutes, or seconds. It is rounded to the nearest day if the offset
Expand All @@ -13,21 +14,21 @@ class << self
# epoch
# @param [Time] arg
# @param [Integer] ms
# @return [Unit, Time]
# @return [RubyUnits::Unit, Time]
def self.at(arg,ms=0)
case arg
when Time
unit_time_at(arg)
when Unit
when RubyUnits::Unit
unit_time_at(arg.convert_to("s").scalar, ms)
else
unit_time_at(arg, ms)
end
end

# @return (see Unit#initialize)
# @return (see RubyUnits::Unit#initialize)
def to_unit(other = nil)
other ? Unit.new(self).convert_to(other) : Unit.new(self)
other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
end
alias :unit :to_unit
alias :u :to_unit
Expand All @@ -39,10 +40,10 @@ def to_unit(other = nil)
end

alias :unit_add :+
# @return [Unit, Time]
# @return [RubyUnits::Unit, Time]
def +(other)
case other
when Unit
when RubyUnits::Unit
other = other.convert_to('d').round.convert_to('s') if ['y', 'decade', 'century'].include? other.units
begin
unit_add(other.convert_to('s').scalar)
Expand All @@ -63,10 +64,10 @@ def self.in(duration)

alias :unit_sub :-

# @return [Unit, Time]
# @return [RubyUnits::Unit, Time]
def -(other)
case other
when Unit
when RubyUnits::Unit
other = other.convert_to('d').round.convert_to('s') if ['y', 'decade', 'century'].include? other.units
begin
unit_sub(other.convert_to('s').scalar)
Expand Down
Loading