Module: Enumerable

Defined in:
lib/backports/1.9/enumerable.rb,
lib/backports/1.8.7/enumerable.rb,
lib/backports/1.8.7/enumerator.rb,
lib/backports/rails/enumerable.rb

Defined Under Namespace

Classes: Enumerator

Constant Summary collapse

MOST_EXTREME_OBJECT_EVER =

:nodoc:

Object.new

Instance Method Summary collapse

Instance Method Details

#count(item = Backports::Undefined) ⇒ Object

Standard in Ruby 1.8.7+. See official documentation



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/backports/1.8.7/enumerable.rb', line 4

def count(item = Backports::Undefined)
  seq = 0
  if item != Backports::Undefined
    each { |o| seq += 1 if item == o }
  elsif block_given?
    each { |o| seq += 1 if yield(o) }
  else
    each { seq += 1 }
  end
  seq
end

#cycle(n = nil, &block) ⇒ Object

Standard in Ruby 1.8.7+. See official documentation



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/backports/1.8.7/enumerable.rb', line 17

def cycle(n = nil, &block)
  return to_enum(:cycle, n) unless block_given?
  return loop(&block) if nil == n
  n = Backports.coerce_to(n, Fixnum, :to_int)
  if n >= 1
    cache = []
    each do |elem|
      cache << elem
      block.call(elem)
    end
    cache.cycle(n-1, &block)
  end
end

#drop(n) ⇒ Object

Standard in Ruby 1.8.7+. See official documentation

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
# File 'lib/backports/1.8.7/enumerable.rb', line 37

def drop(n)
  n = Backports.coerce_to(n, Fixnum, :to_int)
  raise ArgumentError, "attempt to drop negative size" if n < 0
  ary = to_a
  return [] if n > ary.size
  ary[n...ary.size]
end

#drop_while(&block) ⇒ Object

Standard in Ruby 1.8.7+. See official documentation



46
47
48
49
50
51
52
53
54
# File 'lib/backports/1.8.7/enumerable.rb', line 46

def drop_while(&block)
  return to_enum(:drop_while) unless block_given?
  ary = []
  dropping = true
  each do |obj|
    ary << obj unless dropping &&= yield(obj)
  end
  ary
end

#each_with_index_with_optional_args_and_block(*args, &block) ⇒ Object



58
59
60
61
62
63
# File 'lib/backports/1.8.7/enumerable.rb', line 58

def each_with_index_with_optional_args_and_block(*args, &block)
  return to_enum(:each_with_index, *args) unless block_given?
  idx = 0
  each(*args) { |o| yield(o, idx); idx += 1 }
  self
end

#each_with_object(memo, &block) ⇒ Object

Standard in Ruby 1.9. See official documentation



3
4
5
6
7
# File 'lib/backports/1.9/enumerable.rb', line 3

def each_with_object(memo, &block)
  return to_enum(:each_with_object, memo) unless block_given?
  each {|obj| block.call(obj, memo)}
  memo
end

#entries_with_optional_arguments(*args) ⇒ Object



231
232
233
234
# File 'lib/backports/1.8.7/enumerable.rb', line 231

def entries_with_optional_arguments(*args)
  return entries_without_optional_arguments if args.empty?
  to_enum(:each, *args).entries
end

#find_index(obj = Backports::Undefined) ⇒ Object

Standard in Ruby 1.8.7+. See official documentation



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/backports/1.8.7/enumerable.rb', line 68

def find_index(obj = Backports::Undefined)
  if obj != Backports::Undefined
    each_with_index do |element, i|
      return i if element == obj
    end
  elsif block_given?
    each_with_index do |element, i|
      return i if yield element
    end
  else
    return to_enum(:find_index)
  end
  nil
end

#first(n = Backports::Undefined) ⇒ Object

Standard in Ruby 1.8.7+. See official documentation



84
85
86
87
88
# File 'lib/backports/1.8.7/enumerable.rb', line 84

def first(n = Backports::Undefined)
  return take(n) unless n == Backports::Undefined
  each{|obj| return obj}
  nil
end

#group_byObject

Standard in Ruby 1.8.7+. See official documentation



91
92
93
94
95
96
97
98
# File 'lib/backports/1.8.7/enumerable.rb', line 91

def group_by
  return to_enum(:group_by) unless block_given?
  {}.tap do |result|
    each do |o|
      result.fetch(yield(o)){|key| result[key] = []} << o
    end
  end
end

#inject_with_symbol(*args, &block) ⇒ Object



102
103
104
105
106
# File 'lib/backports/1.8.7/enumerable.rb', line 102

def inject_with_symbol(*args, &block)
  return inject_without_symbol(*args, &block) if block_given? && args.size <= 1
  method = args.pop
  inject_without_symbol(*args) {|memo, obj| memo.send(method, obj)}
end

#max_by(&block) ⇒ Object

Standard in Ruby 1.8.7+. See official documentation



122
123
124
125
# File 'lib/backports/1.8.7/enumerable.rb', line 122

def max_by(&block)
  return to_enum(:max_by) unless block_given?
  minmax_by(&block)[1]
end

#min_by(&block) ⇒ Object

Standard in Ruby 1.8.7+. See official documentation



128
129
130
131
# File 'lib/backports/1.8.7/enumerable.rb', line 128

def min_by(&block)
  return to_enum(:min_by) unless block_given?
  minmax_by(&block).first
end

#minmaxObject

Standard in Ruby 1.8.7+. See official documentation



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/backports/1.8.7/enumerable.rb', line 134

def minmax
  return minmax{|a,b| a <=> b} unless block_given?
  first_time = true
  min, max = nil
  each do |object|
    if first_time
      min = max = object
      first_time = false
    else
      min = object if Backports.coerce_to_comparison(min, object, yield(min, object)) > 0
      max = object if Backports.coerce_to_comparison(max, object, yield(max, object)) < 0
    end
  end
  [min, max]
end

#minmax_by(&block) ⇒ Object

Standard in Ruby 1.8.7+. See official documentation



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/backports/1.8.7/enumerable.rb', line 151

def minmax_by(&block)
  return to_enum(:minmax_by) unless block_given?
  min_object, min_result = nil, MOST_EXTREME_OBJECT_EVER
  max_object, max_result = nil, MOST_EXTREME_OBJECT_EVER
  each do |object|
    result = yield object
    min_object, min_result = object, result if min_result > result
    max_object, max_result = object, result if max_result < result
  end
  [min_object, max_object]
end

#none?(&block) ⇒ Boolean

Standard in Ruby 1.8.7+. See official documentation

Returns:

  • (Boolean)


164
165
166
# File 'lib/backports/1.8.7/enumerable.rb', line 164

def none?(&block)
  !any?(&block)
end

#one?(&block) ⇒ Boolean

Standard in Ruby 1.8.7+. See official documentation

Returns:

  • (Boolean)


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/backports/1.8.7/enumerable.rb', line 169

def one?(&block)
  found_one = false
  if block_given?
    each do |o|
      if yield(o)
        return false if found_one
        found_one = true
      end
    end
  else
    each do |o|
      if o
        return false if found_one
        found_one = true
      end
    end
  end
  found_one
end

#reverse_each(&block) ⇒ Object

Standard in Ruby 1.8.7+. See official documentation



192
193
194
195
196
197
# File 'lib/backports/1.8.7/enumerable.rb', line 192

def reverse_each(&block)
  return to_enum(:reverse_each) unless block_given?
  # There is no other way then to convert to an array first... see 1.9's source.
  to_a.reverse_each(&block)
  self
end

#sum(identity = 0, &block) ⇒ Object

Standard in rails… See official documentation Modified from rails 2.3 to not rely on size



4
5
6
7
8
9
10
# File 'lib/backports/rails/enumerable.rb', line 4

def sum(identity = 0, &block)
  if block_given?
    map(&block).sum(identity)
  else
    inject { |sum, element| sum + element } || identity
  end
end

#take(n) ⇒ Object

Standard in Ruby 1.8.7+. See official documentation

Raises:

  • (ArgumentError)


200
201
202
203
204
205
206
207
208
209
# File 'lib/backports/1.8.7/enumerable.rb', line 200

def take(n)
  n = Backports.coerce_to(n, Fixnum, :to_int)
  raise ArgumentError, "attempt to take negative size: #{n}" if n < 0
  [].tap do |array|
    each do |elem|
      array << elem
      break if array.size >= n
    end unless n <= 0
  end
end

#take_whileObject

Standard in Ruby 1.8.7+. See official documentation



212
213
214
215
216
217
218
# File 'lib/backports/1.8.7/enumerable.rb', line 212

def take_while
  return to_enum(:take_while) unless block_given?
  inject([]) do |array, elem|
    return array unless yield elem
    array << elem
  end
end

#to_a_with_optional_arguments(*args) ⇒ Object



222
223
224
225
# File 'lib/backports/1.8.7/enumerable.rb', line 222

def to_a_with_optional_arguments(*args)
  return to_a_without_optional_arguments if args.empty?
  to_enum(:each, *args).to_a
end