Module: Enumerable
- Included in:
- Symbol
- Defined in:
- lib/backports/enumerable.rb
Constant Summary collapse
- MOST_EXTREME_OBJECT_EVER =
:nodoc:
Object.new
Instance Method Summary collapse
-
#count(*arg) ⇒ Object
Standard in ruby 1.9.
-
#cycle(*arg, &block) ⇒ Object
Standard in ruby 1.9.
-
#drop(n) ⇒ Object
Standard in ruby 1.9.
-
#drop_while(&block) ⇒ Object
Standard in ruby 1.9.
- #each_with_index_with_optional_args_and_block(*args, &block) ⇒ Object
-
#each_with_object(memo, &block) ⇒ Object
Standard in ruby 1.9.
-
#find_index(*args) ⇒ Object
Standard in ruby 1.9.
-
#first(*arg) ⇒ Object
Standard in ruby 1.9.
-
#group_by ⇒ Object
Standard in ruby 1.9.
- #inject_with_symbol(*args, &block) ⇒ Object
-
#max_by(&block) ⇒ Object
Standard in ruby 1.9.
-
#min_by(&block) ⇒ Object
Standard in ruby 1.9.
-
#minmax ⇒ Object
Standard in ruby 1.9.
-
#minmax_by(&block) ⇒ Object
Standard in ruby 1.9.
-
#none?(&block) ⇒ Boolean
Standard in ruby 1.9.
-
#one?(&block) ⇒ Boolean
Standard in ruby 1.9.
-
#reverse_each(&block) ⇒ Object
Standard in ruby 1.9.
-
#sum(identity = 0, &block) ⇒ Object
Standard in rails…
-
#take(n) ⇒ Object
Standard in ruby 1.9.
-
#take_while ⇒ Object
Standard in ruby 1.9.
- #to_a_with_optional_arguments(*args) ⇒ Object
Instance Method Details
#count(*arg) ⇒ Object
Standard in ruby 1.9. See official documentation
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/backports/enumerable.rb', line 13 def count(*arg) result = 0 if block_given? each{|o| result += 1 if yield o} elsif arg.empty? each{|o| result += 1} else obj = arg.first each{|o| result += 1 if obj == o} end result end |
#cycle(*arg, &block) ⇒ Object
Standard in ruby 1.9. See official documentation
27 28 29 30 |
# File 'lib/backports/enumerable.rb', line 27 def cycle(*arg, &block) return to_enum(:cycle, *arg) unless block_given? to_a.cycle(*arg, &block) end |
#drop(n) ⇒ Object
Standard in ruby 1.9. See official documentation
35 36 37 38 |
# File 'lib/backports/enumerable.rb', line 35 def drop(n) array = to_a array[n...array.size] || [] end |
#drop_while(&block) ⇒ Object
Standard in ruby 1.9. See official documentation
41 42 43 44 45 46 47 48 |
# File 'lib/backports/enumerable.rb', line 41 def drop_while(&block) return to_enum(:drop_while) unless block_given? array = to_a array.each_with_index do |element, i| return array.drop(i) unless yield(element) end [] end |
#each_with_index_with_optional_args_and_block(*args, &block) ⇒ Object
55 56 57 58 |
# File 'lib/backports/enumerable.rb', line 55 def each_with_index_with_optional_args_and_block(*args, &block) return to_enum(:each_with_index, *args) unless block_given? to_enum(:each, *args).each_with_index_without_optional_args_and_block(&block) end |
#each_with_object(memo, &block) ⇒ Object
Standard in ruby 1.9. See official documentation
63 64 65 66 67 |
# File 'lib/backports/enumerable.rb', line 63 def each_with_object(memo, &block) return to_enum(:each_with_object, memo) unless block_given? each {|obj| block.call(obj, memo)} memo end |
#find_index(*args) ⇒ Object
Standard in ruby 1.9. See official documentation
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/backports/enumerable.rb', line 70 def find_index(*args) raise ArgumentError, "Wrong number of arguments (#{args.size} for 1)" if args.size > 1 if args.size == 1 obj = args.first 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 each_with_index{|o,i| return i if yield o} else return to_enum(:find_index) end nil end |
#first(*arg) ⇒ Object
Standard in ruby 1.9. See official documentation
89 90 91 |
# File 'lib/backports/enumerable.rb', line 89 def first(*arg) arg.empty? ? take(1)[0] : take(arg.first) end |
#group_by ⇒ Object
Standard in ruby 1.9. See official documentation
94 95 96 97 98 99 100 101 |
# File 'lib/backports/enumerable.rb', line 94 def group_by return to_enum(:group_by) unless block_given? returning({}) do |result| each do |o| result.fetch(yield(o)){|key| result[key] = []} << o end end end |
#inject_with_symbol(*args, &block) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/backports/enumerable.rb', line 105 def inject_with_symbol(*args, &block) return inject_without_symbol(*args, &block) if block_given? method = args.pop raise TypeError, "#{method} is not a symbol" unless method.respond_to? :to_sym method = method.to_sym inject_without_symbol(*args) {|memo, obj| memo.send(method, obj)} end |
#max_by(&block) ⇒ Object
Standard in ruby 1.9. See official documentation
127 128 129 130 |
# File 'lib/backports/enumerable.rb', line 127 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.9. See official documentation
133 134 135 136 |
# File 'lib/backports/enumerable.rb', line 133 def min_by(&block) return to_enum(:min_by) unless block_given? minmax_by(&block).first end |
#minmax ⇒ Object
Standard in ruby 1.9. See official documentation
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/backports/enumerable.rb', line 139 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 yield(min, object) > 0 max = object if yield(max, object) < 0 end end [min, max] end |
#minmax_by(&block) ⇒ Object
Standard in ruby 1.9. See official documentation
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/backports/enumerable.rb', line 156 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.9. See official documentation
169 170 171 |
# File 'lib/backports/enumerable.rb', line 169 def none?(&block) !any?(&block) end |
#one?(&block) ⇒ Boolean
Standard in ruby 1.9. See official documentation
174 175 176 177 |
# File 'lib/backports/enumerable.rb', line 174 def one?(&block) return one?{|o| o} unless block_given? 1 == count(&block) end |
#reverse_each(&block) ⇒ Object
Standard in ruby 1.9. See official documentation
182 183 184 185 186 187 |
# File 'lib/backports/enumerable.rb', line 182 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/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.9. See official documentation
190 191 192 193 194 195 196 197 |
# File 'lib/backports/enumerable.rb', line 190 def take(n) returning([]) do |array| each do |elem| array << elem break if array.size >= n end unless n <= 0 end end |
#take_while ⇒ Object
Standard in ruby 1.9. See official documentation
200 201 202 203 204 205 206 |
# File 'lib/backports/enumerable.rb', line 200 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
210 211 212 213 |
# File 'lib/backports/enumerable.rb', line 210 def to_a_with_optional_arguments(*args) return to_a_without_optional_arguments if args.empty? to_enum(:each, *args).to_a end |