Class: Enumerator.self::ArithmeticSequence
- Defined in:
- opal/opal/corelib/enumerator/arithmetic_sequence.rb
Instance Attribute Summary collapse
-
#step ⇒ Object
readonly
Returns the value of attribute step.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #===, #eql?)
- #_greater_than_begin?(val) ⇒ Boolean
- #_lesser_than_end?(val) ⇒ Boolean
- #begin ⇒ Object
- #each(&block) ⇒ Object
- #end ⇒ Object
- #exclude_end? ⇒ Boolean
- #first(count = undefined) ⇒ Object
- #hash ⇒ Object
-
#initialize(range, step = undefined, creation_method = :step) ⇒ ArithmeticSequence
constructor
A new instance of ArithmeticSequence.
- #inspect ⇒ Object
- #last(count = undefined) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(range, step = undefined, creation_method = :step) ⇒ ArithmeticSequence
Returns a new instance of ArithmeticSequence.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 10 def initialize(range, step = undefined, creation_method = :step) @creation_method = creation_method if range.is_a? ::Array @step_arg1, @step_arg2, @topfx, @bypfx = *range @receiver_num = step @step = 1 @range = if @step_arg2 @step = @step_arg2 (@receiver_num..@step_arg1) elsif @step_arg1 (@receiver_num..@step_arg1) else (@receiver_num..nil) end else @skipped_arg = true unless step @range, @step = range, step || 1 end @object = self ::Kernel.raise ArgumentError, "step can't be 0" if @step == 0 unless @step.respond_to? :to_int ::Kernel.raise ArgumentError, "no implicit conversion of #{@step.class} " \ 'into Integer' end end |
Instance Attribute Details
#step ⇒ Object (readonly)
Returns the value of attribute step.
39 40 41 |
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 39 def step @step end |
Instance Method Details
#==(other) ⇒ Object Also known as: ===, eql?
147 148 149 150 151 152 153 |
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 147 def ==(other) self.class == other.class && self.begin == other.begin && self.end == other.end && step == other.step && exclude_end? == other.exclude_end? end |
#_greater_than_begin?(val) ⇒ Boolean
64 65 66 67 68 69 70 71 |
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 64 def _greater_than_begin?(val) begin_ = self.begin || -`inf` if step > 0 val > begin_ else val < begin_ end end |
#_lesser_than_end?(val) ⇒ Boolean
54 55 56 57 58 59 60 61 |
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 54 def _lesser_than_end?(val) end_ = self.end || `inf` if step > 0 exclude_end? ? val < end_ : val <= end_ else exclude_end? ? val > end_ : val >= end_ end end |
#begin ⇒ Object
41 42 43 |
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 41 def begin @range.begin end |
#each(&block) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 89 def each(&block) return self unless block_given? case self.begin when nil ::Kernel.raise TypeError, "nil can't be coerced into Integer" end iter = self.begin || -`inf` while _lesser_than_end?(iter) yield iter iter += step end self end |
#end ⇒ Object
45 46 47 |
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 45 def end @range.end end |
#exclude_end? ⇒ Boolean
49 50 51 |
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 49 def exclude_end? @range.exclude_end? end |
#first(count = undefined) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 73 def first(count = undefined) iter = self.begin || -`inf` return _lesser_than_end?(iter) ? iter : nil unless count out = [] while _lesser_than_end?(iter) && count > 0 out << iter iter += step count -= 1 end out end |
#hash ⇒ Object
155 156 157 |
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 155 def hash [ArithmeticSequence, self.begin, self.end, step, exclude_end?].hash end |
#inspect ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 159 def inspect if @receiver_num args = if @step_arg2 "(#{@topfx}#{@step_arg1.inspect}, #{@bypfx}#{@step_arg2.inspect})" elsif @step_arg1 "(#{@topfx}#{@step_arg1.inspect})" end "(#{@receiver_num.inspect}.#{@creation_method}#{args})" else args = unless @skipped_arg "(#{@step})" end "((#{@range.inspect}).#{@creation_method}#{args})" end end |
#last(count = undefined) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 106 def last(count = undefined) case self.end when `inf`, -`inf` ::Kernel.raise ::FloatDomainError, self.end when nil ::Kernel.raise ::RangeError, 'cannot get the last element of endless arithmetic sequence' end iter = self.end - ((self.end - self.begin) % step) iter -= step unless _lesser_than_end?(iter) return _greater_than_begin?(iter) ? iter : nil unless count out = [] while _greater_than_begin?(iter) && count > 0 out << iter iter -= step count -= 1 end out.reverse end |
#size ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 130 def size step_sign = step > 0 ? 1 : -1 if !_lesser_than_end?(self.begin) 0 elsif [-`inf`, `inf`].include?(step) 1 elsif [-`inf` * step_sign, nil].include?(self.begin) || [`inf` * step_sign, nil].include?(self.end) `inf` else iter = self.end - ((self.end - self.begin) % step) iter -= step unless _lesser_than_end?(iter) ((iter - self.begin) / step).abs.to_i + 1 end end |