Class: Enumerable::LinkedListDelegator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/agents/sets/enum/by.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#each_cluster, #each_with_neighbors, #group, nest, #nest, #pipe

Constructor Details

#initialize(first, next_spec = nil, *args, &next_proc) ⇒ LinkedListDelegator

Returns a new instance of LinkedListDelegator.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/agents/sets/enum/by.rb', line 10

def initialize first, next_spec = nil, *args, &next_proc
  @first = first
  @args = args
  
  case next_spec
  when Symbol
    @next_name = next_spec
  when String
    @next_name = next_spec.intern
  when nil
    @next_map = next_proc
  else
    unless next_spec.respond_to? :[]
      raise ArgumentError,
        "next_spec must be a method name or respond to []."
    end
    @next_map = next_spec
  end
  
  unless @next_name or @next_map
    raise ArgumentError,
      "no next-getter specified."
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



8
9
10
# File 'lib/agents/sets/enum/by.rb', line 8

def args
  @args
end

#firstObject (readonly)

Returns the value of attribute first.



8
9
10
# File 'lib/agents/sets/enum/by.rb', line 8

def first
  @first
end

#next_mapObject (readonly)

Returns the value of attribute next_map.



8
9
10
# File 'lib/agents/sets/enum/by.rb', line 8

def next_map
  @next_map
end

#next_nameObject (readonly)

Returns the value of attribute next_name.



8
9
10
# File 'lib/agents/sets/enum/by.rb', line 8

def next_name
  @next_name
end

Instance Method Details

#eachObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/agents/sets/enum/by.rb', line 35

def each
  cur = @first
  if @next_name
    next_name = @next_name
    message = next_name, *@args
    while cur
      yield cur
      cur = cur.send *message
    end
  elsif @next_map
    next_map = @next_map
    args = @args
    while cur
      yield cur
      cur = next_map[cur, *args]
    end
  end

  self
end