Class: Range

Inherits:
Object
  • 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, #find_all, #find_index, #grep, #group_by, #inject, #lazy, #max_by, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #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



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

def initialize(first, last, exclude = false)
  @begin   = first
  @end     = last
  @exclude = exclude
end

Instance Attribute Details

#beginObject (readonly) Also known as: first

Returns the value of attribute begin



6
7
8
# File 'opal/opal/corelib/range.rb', line 6

def begin
  @begin
end

#endObject (readonly) Also known as: last

Returns the value of attribute end



6
7
8
# File 'opal/opal/corelib/range.rb', line 6

def end
  @end
end

Instance Method Details

#==(other) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'opal/opal/corelib/range.rb', line 14

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

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

#===(obj) ⇒ Object

FIXME: currently hardcoded to assume range holds numerics



27
28
29
# File 'opal/opal/corelib/range.rb', line 27

def ===(obj)
  include?(obj)
end

#cover?(value) ⇒ Boolean

Returns:



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

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

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

Yields:

  • (current)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'opal/opal/corelib/range.rb', line 37

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

  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:



54
55
56
57
58
59
60
# File 'opal/opal/corelib/range.rb', line 54

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:



62
63
64
# File 'opal/opal/corelib/range.rb', line 62

def exclude_end?
  @exclude
end

#include?(obj) ⇒ Boolean Also known as: member?

FIXME: currently hardcoded to assume range holds numerics

Returns:



69
70
71
# File 'opal/opal/corelib/range.rb', line 69

def include?(obj)
  cover?(obj)
end

#maxObject

FIXME: currently hardcoded to assume range holds numerics



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

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

#minObject



82
83
84
85
86
87
88
# File 'opal/opal/corelib/range.rb', line 82

def min
  if block_given?
    super
  else
    @begin
  end
end

#step(n = 1) ⇒ Object



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

def step(n = 1)
  raise NotImplementedError
end

#to_sObject Also known as: inspect



96
97
98
# File 'opal/opal/corelib/range.rb', line 96

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