Class: Range

Inherits:
Object show all
Includes:
Enumerable
Defined in:
opal/opal/corelib/range.rb,
opal/opal/corelib/marshal/write_buffer.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

#__marshal__(buffer) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 145

def __marshal__(buffer)
  buffer.save_link(self)
  buffer.write_extends(self)
  buffer.append("o")
  buffer.append_symbol(self.class.name)
  buffer.write_fixnum(3)
  buffer.append_symbol('excl')
  buffer.write(exclude_end?)
  buffer.append_symbol('begin')
  buffer.write(self.begin)
  buffer.append_symbol('end')
  buffer.write(self.end)
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
# File 'opal/opal/corelib/range.rb', line 38

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

  %x{
    var i, limit;

    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++) {
        block(i);
      }

      return self;
    }

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

  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:



76
77
78
79
80
81
82
# File 'opal/opal/corelib/range.rb', line 76

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:



84
85
86
# File 'opal/opal/corelib/range.rb', line 84

def exclude_end?
  @exclude
end

#marshal_load(args) ⇒ Object



138
139
140
141
142
# File 'opal/opal/corelib/range.rb', line 138

def marshal_load(args)
  @begin = args[:begin]
  @end = args[:end]
  @exclude = args[:excl]
end

#maxObject

FIXME: currently hardcoded to assume range holds numerics



95
96
97
98
99
100
101
# File 'opal/opal/corelib/range.rb', line 95

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

#minObject



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

def min
  if block_given?
    super
  else
    @begin
  end
end

#sizeObject



115
116
117
118
119
120
121
122
123
124
125
126
# File 'opal/opal/corelib/range.rb', line 115

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



128
129
130
# File 'opal/opal/corelib/range.rb', line 128

def step(n = 1)
  raise NotImplementedError
end

#to_sObject Also known as: inspect



132
133
134
# File 'opal/opal/corelib/range.rb', line 132

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