ruby-units 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ # make a unit from an array
2
+ # [1, 'mm'].unit => 1 mm
3
+ class Array
4
+ def to_unit(other = nil)
5
+ other ? Unit.new(self).to(other) : Unit.new(self)
6
+ end
7
+ alias :unit :to_unit
8
+ alias :u :to_unit
9
+ end
@@ -0,0 +1,10 @@
1
+ class Complex < Numeric
2
+ def to_unit(other = nil)
3
+ real_unit = self.real.to_unit
4
+ image_unit = self.image.to_unit
5
+ raise ArgumentError, 'Units on real and imaginary parts are incompatible' unless real_unit =~ image_unit
6
+ final_unit = (real_unit.units.empty? ? image_unit.units : real_unit.units).to_unit
7
+ final_unit * Complex(real_unit.to(final_unit).scalar, image_unit.to(final_unit).scalar)
8
+ end
9
+ alias :unit :to_unit
10
+ end
@@ -0,0 +1,51 @@
1
+ # Allow date objects to do offsets by a time unit
2
+ # Date.today + U"1 week" => gives today+1 week
3
+
4
+ require 'date'
5
+
6
+ class Date
7
+ alias :unit_date_add :+
8
+ def +(unit)
9
+ case unit
10
+ when Unit:
11
+ unit = unit.to('d').round if ['y', 'decade', 'century'].include? unit.units
12
+ unit_date_add(unit.to('day').scalar)
13
+ when Time: unit_date_add(unit.to_datetime)
14
+ else
15
+ unit_date_add(unit)
16
+ end
17
+ end
18
+
19
+
20
+ alias :unit_date_sub :-
21
+ def -(unit)
22
+ case unit
23
+ when Unit:
24
+ unit = unit.to('d').round if ['y', 'decade', 'century'].include? unit.units
25
+ unit_date_sub(unit.to('day').scalar)
26
+ when Time: unit_date_sub(unit.to_datetime)
27
+ else
28
+ unit_date_sub(unit)
29
+ end
30
+ end
31
+
32
+ def to_unit(other = nil)
33
+ other ? Unit.new(self).to(other) : Unit.new(self)
34
+ end
35
+ alias :unit :to_unit
36
+
37
+ def to_time
38
+ Time.local(*ParseDate.parsedate(self.to_s))
39
+ end
40
+
41
+ alias :units_datetime_inspect :inspect
42
+ def inspect(raw = false)
43
+ return self.units_datetime_inspect if raw
44
+ self.to_s
45
+ end
46
+
47
+ def to_date
48
+ Date.civil(self.year, self.month, self.day)
49
+ end
50
+
51
+ end
@@ -0,0 +1,87 @@
1
+ # Math will convert unit objects to radians and then attempt to use the value for
2
+ # trigonometric functions.
3
+
4
+ module Math
5
+ alias unit_sqrt sqrt
6
+ def sqrt(n)
7
+ if Unit === n
8
+ (n**(1/2)).to_unit
9
+ else
10
+ unit_sqrt(n)
11
+ end
12
+ end
13
+
14
+ alias unit_sin sin
15
+ def sin(n)
16
+ Unit === n ? unit_sin(n.to('radian').scalar) : unit_sin(n)
17
+ end
18
+
19
+ alias unit_cos cos
20
+ def cos(n)
21
+ Unit === n ? unit_cos(n.to('radian').scalar) : unit_cos(n)
22
+ end
23
+
24
+ alias unit_sinh sinh
25
+ def sinh(n)
26
+ Unit === n ? unit_sinh(n.to('radian').scalar) : unit_sinh(n)
27
+ end
28
+
29
+ alias unit_cosh cosh
30
+ def cosh(n)
31
+ Unit === n ? unit_cosh(n.to('radian').scalar) : unit_cosh(n)
32
+ end
33
+
34
+ alias unit_tan tan
35
+ def tan(n)
36
+ Unit === n ? unit_tan(n.to('radian').scalar) : unit_tan(n)
37
+ end
38
+
39
+ alias unit_tanh tanh
40
+ def tanh(n)
41
+ Unit === n ? unit_tanh(n.to('radian').scalar) : unit_tanh(n)
42
+ end
43
+
44
+ alias unit_hypot hypot
45
+ # Convert parameters to consistent units and perform the function
46
+ def hypot(x,y)
47
+ if Unit === x && Unit === y
48
+ (x**2 + y**2)**(1/2)
49
+ else
50
+ unit_hypot(x,y)
51
+ end
52
+ end
53
+
54
+ alias unit_atan2 atan2
55
+ def atan2(x,y)
56
+ case
57
+ when (Unit === x && Unit === y) && (x !~ y)
58
+ raise ArgumentError, "Incompatible Units"
59
+ when (Unit === x && Unit === y) && (x =~ y)
60
+ unit_atan2(x.base_scalar, y.base_scalar)
61
+ when (Unit === x || Unit === y)
62
+ raise ArgumentError, "Incompatible Units"
63
+ else
64
+ unit_atan2(x,y)
65
+ end
66
+ end
67
+
68
+ module_function :unit_hypot
69
+ module_function :hypot
70
+ module_function :unit_sqrt
71
+ module_function :sqrt
72
+ module_function :unit_sin
73
+ module_function :sin
74
+ module_function :unit_cos
75
+ module_function :cos
76
+ module_function :unit_sinh
77
+ module_function :sinh
78
+ module_function :unit_cosh
79
+ module_function :cosh
80
+ module_function :unit_tan
81
+ module_function :tan
82
+ module_function :unit_tanh
83
+ module_function :tanh
84
+ module_function :unit_atan2
85
+ module_function :atan2
86
+
87
+ end
@@ -0,0 +1,8 @@
1
+ # make a unitless unit with a given scalar
2
+ class Numeric
3
+ def to_unit(other = nil)
4
+ other ? Unit.new(self, other) : Unit.new(self)
5
+ end
6
+ alias :unit :to_unit
7
+ alias :u :to_unit
8
+ end
@@ -0,0 +1,8 @@
1
+ class Object
2
+ def Unit(*other)
3
+ other.to_unit
4
+ end
5
+
6
+ alias :U :Unit
7
+ alias :u :Unit
8
+ end