Class: Range

Inherits:
Object show all
Includes:
Enumerable
Defined in:
opal/opal/corelib/range.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#all?, #any?, #chunk, #collect, #collect_concat, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #enumerator_size, #find_all, #find_index, #grep, #group_by, #inject, #lazy, #max_by, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #slice_before, #sort, #sort_by, #take, #take_while, #zip

Constructor Details

#initialize(first, last, exclude = false) ⇒ Range

Returns a new instance of Range

Raises:



10
11
12
13
14
15
16
# File 'opal/opal/corelib/range.rb', line 10

def initialize(first, last, exclude = false)
  raise ArgumentError unless first <=> last

  @begin   = first
  @end     = last
  @exclude = exclude
end

Instance Attribute Details

#beginObject (readonly) Also known as: first

Returns the value of attribute begin



8
9
10
# File 'opal/opal/corelib/range.rb', line 8

def begin
  @begin
end

#endObject (readonly) Also known as: last

Returns the value of attribute end



8
9
10
# File 'opal/opal/corelib/range.rb', line 8

def end
  @end
end

Instance Method Details

#==(other) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'opal/opal/corelib/range.rb', line 18

def ==(other)
  %x{
    if (!other.$$is_range) {
      return false;
    }

    return self.exclude === other.exclude &&
           self.begin   ==  other.begin &&
           self.end     ==  other.end;
  }
end

#===(value) ⇒ Object



30
31
32
# File 'opal/opal/corelib/range.rb', line 30

def ===(value)
  include? value
end

#cover?(value) ⇒ Boolean Also known as: include?

Returns:



34
35
36
# File 'opal/opal/corelib/range.rb', line 34

def cover?(value)
  @begin <= value && (@exclude ? value < @end : value <= @end)
end

#each {|current| ... } ⇒ Object

Yields:

  • (current)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'opal/opal/corelib/range.rb', line 38

def each(&block)
  return enum_for :each unless block_given?

  %x{
    var i, limit, value;

    if (#@begin.$$is_number && #@end.$$is_number) {
      if (#@begin % 1 !== 0 || #@end % 1 !== 0) {
        #{raise TypeError, "can't iterate from Float"}
      }

      for (i = #@begin, limit = #@end + #{@exclude ? 0 : 1}; i < limit; i++) {
        value = block(i);
        if (value === $breaker) { return $breaker.$v; }
      }

      return self;
    }

    if (#@begin.$$is_string && #@end.$$is_string) {
      value = #{@begin.upto(@end, @exclude, &block)};

      // The following is a bit hackish: we know that
      // String#upto normally returns self, but may
      // return a different value if there's a `break`
      // statement in the supplied block. We need to
      // propagate this `break` value here, so we
      // test for equality with `@begin` string to
      // determine the return value:
      return value === #@begin ? self : value;
    }
  }

  current = @begin
  last    = @end

  while current < last
    yield current

    current = current.succ
  end

  yield current if !@exclude && current == last

  self
end

#eql?(other) ⇒ Boolean

Returns:



85
86
87
88
89
90
91
# File 'opal/opal/corelib/range.rb', line 85

def eql?(other)
  return false unless Range === other

  @exclude === other.exclude_end? &&
  @begin.eql?(other.begin) &&
  @end.eql?(other.end)
end

#exclude_end?Boolean

Returns:



93
94
95
# File 'opal/opal/corelib/range.rb', line 93

def exclude_end?
  @exclude
end

#maxObject

FIXME: currently hardcoded to assume range holds numerics



104
105
106
107
108
109
110
# File 'opal/opal/corelib/range.rb', line 104

def max
  if block_given?
    super
  else
    `#@exclude ? #@end - 1 : #@end`
  end
end

#minObject



114
115
116
117
118
119
120
# File 'opal/opal/corelib/range.rb', line 114

def min
  if block_given?
    super
  else
    @begin
  end
end

#sizeObject



124
125
126
127
128
129
130
131
132
133
134
135
# File 'opal/opal/corelib/range.rb', line 124

def size
  _begin = @begin
  _end   = @end
  _end  -= 1 if @exclude

  return nil unless Numeric === _begin && Numeric === _end
  return 0 if _end < _begin
  infinity = Float::INFINITY
  return infinity if infinity == _begin.abs || _end.abs == infinity

  (`Math.abs(_end - _begin) + 1`).to_i
end

#step(n = 1) ⇒ Object



137
138
139
# File 'opal/opal/corelib/range.rb', line 137

def step(n = 1)
  raise NotImplementedError
end

#to_sObject Also known as: inspect



141
142
143
# File 'opal/opal/corelib/range.rb', line 141

def to_s
  `#{@begin.inspect} + (#@exclude ? '...' : '..') + #{@end.inspect}`
end