| 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 |
# Integer |
| 38 |
# |
| 39 |
# ISO 15.2.8 |
| 40 |
## |
| 41 |
class Integer |
| 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 |
| 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 |
| 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 |
| 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 |
| 106 |
|
| 107 |
i = __coerce_step_counter(num, step) |
| 108 |
if num == self || step.infinite? |
| 109 |
block.call(i) if step > 0 && i <= (num||i) || step < 0 && i >= (num||-i) |
| 110 |
elsif num == nil |
| 111 |
while true |
| 112 |
block.call(i) |
| 113 |
i += step |
| 114 |
end |
| 115 |
elsif 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 |
class Float |
| 131 |
## |
| 132 |
# Calls the given block from +self+ to +num+ |
| 133 |
# incremented by +step+ (default 1). |
| 134 |
# |
| 135 |
def step(num=nil, step=1, &block) |
| 136 |
raise ArgumentError, "step can't be 0" if step == 0 |
| 137 |
return to_enum(:step, num, step) unless block |
| 138 |
|
| 139 |
i = self |
| 140 |
if num == self || step.infinite? |
| 141 |
block.call(i) if step > 0 && i <= (num||i) || step < 0 && i >= (num||-i) |
| 142 |
elsif num == nil |
| 143 |
while true |
| 144 |
block.call(i) |
| 145 |
i += step |
| 146 |
end |
| 147 |
elsif step > 0 |
| 148 |
while i <= num |
| 149 |
block.call(i) |
| 150 |
i += step |
| 151 |
end |
| 152 |
else |
| 153 |
while i >= num |
| 154 |
block.call(i) |
| 155 |
i += step |
| 156 |
end |
| 157 |
end |
| 158 |
self |
| 159 |
end |
| 160 |
end if Object.const_defined?(:Float) |