epitools 0.5.133 → 0.5.136

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,78 +0,0 @@
1
- #
2
- # The ratio between two numbers (eg: 2:1, 3:4)
3
- #
4
- # Can be compared, added, "percent"ed, "to_f"ed, and displayed.
5
- #
6
- class Ratio
7
-
8
- include Comparable
9
-
10
- def <=>(other)
11
- to_f <=> other.to_f
12
- end
13
-
14
- attr_accessor :first, :last
15
-
16
- def self.[](*args)
17
- new(*args)
18
- end
19
-
20
- #
21
- # `first` is the top part of the ratio, `last` is the bottom (eg: `first/last`)
22
- #
23
- def initialize(first, last=1)
24
- @first = first
25
- @last = last
26
- end
27
-
28
- #
29
- # Returns a string representation: "a/b"
30
- #
31
- def to_s
32
- "#{@first}/#{@last}"
33
- end
34
- alias_method :ratio, :to_s
35
-
36
- #
37
- # Returns the ratio as a float. (eg: Ratio[1,2].to_f == 0.5)
38
- #
39
- def to_f
40
- if @last == 0
41
- 0.0
42
- else
43
- @first.to_f / @last
44
- end
45
- end
46
-
47
- #
48
- # Returns a string representing the number in percent
49
- #
50
- def percent
51
- "%0.1f%%" % (to_f * 100)
52
- end
53
- alias_method :to_percent, :percent
54
-
55
- #
56
- # "#<Ratio: 1/2>"
57
- #
58
- def inspect
59
- "#<Ratio: #{to_s}>"
60
- end
61
-
62
- #
63
- # Adds together the tops and bottoms of the ratios.
64
- #
65
- # Example: For the ratios `a/c` and `b/d`, returns `a+b/c+d`
66
- #
67
- def +(other)
68
- Ratio.new( first+other.first, last+other.last)
69
- end
70
-
71
- end
72
-
73
- #
74
- # Function-style wrapper
75
- #
76
- def Ratio(*args)
77
- Ratio.new(*args)
78
- end
File without changes
File without changes