Class: Enumerator.self::ArithmeticSequence

Inherits:
Enumerator
  • Object
show all
Defined in:
opal/opal/corelib/enumerator/arithmetic_sequence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range, step = undefined, creation_method = :step) ⇒ ArithmeticSequence

Returns a new instance of ArithmeticSequence.



8
9
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
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 8

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

#stepObject (readonly)

Returns the value of attribute step.



37
38
39
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 37

def step
  @step
end

Instance Method Details

#==(other) ⇒ Object Also known as: ===, eql?



145
146
147
148
149
150
151
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 145

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

Returns:



62
63
64
65
66
67
68
69
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 62

def _greater_than_begin?(val)
  begin_ = self.begin || -`inf`
  if step > 0
    val > begin_
  else
    val < begin_
  end
end

#_lesser_than_end?(val) ⇒ Boolean

Returns:



52
53
54
55
56
57
58
59
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 52

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

#beginObject



39
40
41
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 39

def begin
  @range.begin
end

#each(&block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 87

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

#endObject



43
44
45
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 43

def end
  @range.end
end

#exclude_end?Boolean

Returns:



47
48
49
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 47

def exclude_end?
  @range.exclude_end?
end

#first(count = undefined) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 71

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

#hashObject



153
154
155
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 153

def hash
  [self.begin, self.end, step, exclude_end?].hash
end

#inspectObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 157

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 104

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

#sizeObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'opal/opal/corelib/enumerator/arithmetic_sequence.rb', line 128

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