Module: RIEL::EnumerableExt

Defined in:
lib/riel/enumerable.rb

Constant Summary collapse

NOT_NIL =
Object.new

Instance Method Summary collapse

Instance Method Details

#_match?(arg, item, idx, &blk) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/riel/enumerable.rb', line 31

def _match? arg, item, idx, &blk
  if blk 
    args = [ item ]
    args << idx if idx && blk.arity > 1
    blk.call(*args)
  elsif arg == NOT_NIL
    !item.nil?
  else
    arg == item
  end
end

#collect_with_index(&blk) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/riel/enumerable.rb', line 6

def collect_with_index &blk
  ary = []
  self.each_with_index do |item, idx|
    ary << blk.call(item, idx)
  end
  ary
end

#detect_with_index(arg = NOT_NIL, &blk) ⇒ Object



24
25
26
27
28
29
# File 'lib/riel/enumerable.rb', line 24

def detect_with_index arg = NOT_NIL, &blk
  self.each_with_index do |item, idx|
    return item if _match?(arg, item, idx, &blk)
  end
  nil
end

#select_with_index(arg = NOT_NIL, &blk) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/riel/enumerable.rb', line 16

def select_with_index arg = NOT_NIL, &blk
  ary = []
  self.each_with_index do |item, idx|
    ary << item if _match?(arg, item, idx, &blk)
  end
  ary
end