1 | ##
|
---|
2 | # Numeric
|
---|
3 | #
|
---|
4 | # ISO 15.2.7
|
---|
5 | class Numeric
|
---|
6 | include Comparable
|
---|
7 | ##
|
---|
8 | # Returns the receiver simply.
|
---|
9 | #
|
---|
10 | # ISO 15.2.7.4.1
|
---|
11 | def +@
|
---|
12 | self
|
---|
13 | end
|
---|
14 |
|
---|
15 | ##
|
---|
16 | # Returns the receiver's value, negated.
|
---|
17 | #
|
---|
18 | # ISO 15.2.7.4.2
|
---|
19 | def -@
|
---|
20 | 0 - self
|
---|
21 | end
|
---|
22 |
|
---|
23 | ##
|
---|
24 | # Returns the absolute value of the receiver.
|
---|
25 | #
|
---|
26 | # ISO 15.2.7.4.3
|
---|
27 | def abs
|
---|
28 | if self < 0
|
---|
29 | -self
|
---|
30 | else
|
---|
31 | self
|
---|
32 | end
|
---|
33 | end
|
---|
34 | end
|
---|
35 |
|
---|
36 | ##
|
---|
37 | # Integral
|
---|
38 | #
|
---|
39 | # mruby special - module to share methods between Floats and Integers
|
---|
40 | # to make them compatible
|
---|
41 | module Integral
|
---|
42 | ##
|
---|
43 | # Calls the given block once for each Integer
|
---|
44 | # from +self+ downto +num+.
|
---|
45 | #
|
---|
46 | # ISO 15.2.8.3.15
|
---|
47 | def downto(num, &block)
|
---|
48 | return to_enum(:downto, num) unless block_given?
|
---|
49 |
|
---|
50 | i = self.to_i
|
---|
51 | while i >= num
|
---|
52 | block.call(i)
|
---|
53 | i -= 1
|
---|
54 | end
|
---|
55 | self
|
---|
56 | end
|
---|
57 |
|
---|
58 | ##
|
---|
59 | # Returns self + 1
|
---|
60 | #
|
---|
61 | # ISO 15.2.8.3.19
|
---|
62 | def next
|
---|
63 | self + 1
|
---|
64 | end
|
---|
65 | # ISO 15.2.8.3.21
|
---|
66 | alias succ next
|
---|
67 |
|
---|
68 | ##
|
---|
69 | # Calls the given block +self+ times.
|
---|
70 | #
|
---|
71 | # ISO 15.2.8.3.22
|
---|
72 | def times &block
|
---|
73 | return to_enum :times unless block_given?
|
---|
74 |
|
---|
75 | i = 0
|
---|
76 | while i < self
|
---|
77 | block.call i
|
---|
78 | i += 1
|
---|
79 | end
|
---|
80 | self
|
---|
81 | end
|
---|
82 |
|
---|
83 | ##
|
---|
84 | # Calls the given block once for each Integer
|
---|
85 | # from +self+ upto +num+.
|
---|
86 | #
|
---|
87 | # ISO 15.2.8.3.27
|
---|
88 | def upto(num, &block)
|
---|
89 | return to_enum(:upto, num) unless block_given?
|
---|
90 |
|
---|
91 | i = self.to_i
|
---|
92 | while i <= num
|
---|
93 | block.call(i)
|
---|
94 | i += 1
|
---|
95 | end
|
---|
96 | self
|
---|
97 | end
|
---|
98 |
|
---|
99 | ##
|
---|
100 | # Calls the given block from +self+ to +num+
|
---|
101 | # incremented by +step+ (default 1).
|
---|
102 | #
|
---|
103 | def step(num=nil, step=1, &block)
|
---|
104 | raise ArgumentError, "step can't be 0" if step == 0
|
---|
105 | return to_enum(:step, num, step) unless block_given?
|
---|
106 |
|
---|
107 | i = if num.kind_of? Float then self.to_f else self end
|
---|
108 | if num == nil
|
---|
109 | while true
|
---|
110 | block.call(i)
|
---|
111 | i+=step
|
---|
112 | end
|
---|
113 | return self
|
---|
114 | end
|
---|
115 | if step > 0
|
---|
116 | while i <= num
|
---|
117 | block.call(i)
|
---|
118 | i += step
|
---|
119 | end
|
---|
120 | else
|
---|
121 | while i >= num
|
---|
122 | block.call(i)
|
---|
123 | i += step
|
---|
124 | end
|
---|
125 | end
|
---|
126 | self
|
---|
127 | end
|
---|
128 | end
|
---|
129 |
|
---|
130 | ##
|
---|
131 | # Integer
|
---|
132 | #
|
---|
133 | # ISO 15.2.8
|
---|
134 | class Integer
|
---|
135 | include Integral
|
---|
136 | ##
|
---|
137 | # Returns the receiver simply.
|
---|
138 | #
|
---|
139 | # ISO 15.2.8.3.14
|
---|
140 | def ceil
|
---|
141 | self
|
---|
142 | end
|
---|
143 |
|
---|
144 | ##
|
---|
145 | # Returns the receiver simply.
|
---|
146 | #
|
---|
147 | # ISO 15.2.8.3.17
|
---|
148 | def floor
|
---|
149 | self
|
---|
150 | end
|
---|
151 |
|
---|
152 | ##
|
---|
153 | # Returns the receiver simply.
|
---|
154 | #
|
---|
155 | # ISO 15.2.8.3.24
|
---|
156 | alias round floor
|
---|
157 |
|
---|
158 | ##
|
---|
159 | # Returns the receiver simply.
|
---|
160 | #
|
---|
161 | # ISO 15.2.8.3.26
|
---|
162 | alias truncate floor
|
---|
163 | end
|
---|
164 |
|
---|
165 | ##
|
---|
166 | # Float
|
---|
167 | #
|
---|
168 | # ISO 15.2.9
|
---|
169 | class Float
|
---|
170 | # mruby special - since mruby integers may be upgraded to floats,
|
---|
171 | # floats should be compatible to integers.
|
---|
172 | include Integral
|
---|
173 | end
|
---|