Class: Enumerator

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

Direct Known Subclasses

Lazy

Defined Under Namespace

Classes: Generator, Lazy, Yielder

Class Method 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, #first, #grep, #group_by, #include?, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #slice_before, #sort, #sort_by, #take, #take_while, #zip

Constructor Details

#initialize(&block) ⇒ Enumerator

Returns a new instance of Enumerator



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'opal/opal/corelib/enumerator.rb', line 20

def initialize(*, &block)
  if block
    @object = Generator.new(&block)
    @method = :each
    @args   = []
    @size   = `arguments[0] || nil`

    if @size
      @size = Opal.coerce_to @size, Integer, :to_int
    end
  else
    @object = `arguments[0]`
    @method = `arguments[1] || "each"`
    @args   = `$slice.call(arguments, 2)`
    @size   = nil
  end
end

Class Method Details

.for(object, method = :each, *args, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'opal/opal/corelib/enumerator.rb', line 6

def self.for(object, method = :each, *args, &block)
  %x{
    var obj = #{allocate};

    obj.object = object;
    obj.size   = block;
    obj.method = method;
    obj.args   = args;

    return obj;
  }

end

Instance Method Details

#each(*args, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'opal/opal/corelib/enumerator.rb', line 38

def each(*args, &block)
  return self if block.nil? && args.empty?

  args = @args + args

  return self.class.new(@object, @method, *args) if block.nil?

  @object.__send__(@method, *args, &block)
end

#inspectObject



88
89
90
91
92
93
94
95
96
# File 'opal/opal/corelib/enumerator.rb', line 88

def inspect
  result = "#<#{self.class}: #{@object.inspect}:#{@method}"

  unless @args.empty?
    result += "(#{@args.inspect[Range.new(1, -2)]})"
  end

  result + ">"
end

#sizeObject



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

def size
  Proc === @size ? @size.call(*@args) : @size
end

#with_index(offset = 0, &block) ⇒ Object



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
84
# File 'opal/opal/corelib/enumerator.rb', line 52

def with_index(offset = 0, &block)
  if offset
    offset = Opal.coerce_to offset, Integer, :to_int
  else
    offset = 0
  end

  return enum_for :with_index, offset unless block

  %x{
    var result, index = offset;

    self.$each.$$p = function() {
      var param = #{Opal.destructure(`arguments`)},
          value = block(param, index);

      if (value === $breaker) {
        result = $breaker.$v;
        return $breaker;
      }

      index++;
    }

    self.$each();

    if (result !== undefined) {
      return result;
    }

    return self.object;
  }
end