Module: Enumerable
- Defined in:
- lib/rubyc/core_extensions.rb
Instance Method Summary collapse
- #count_by ⇒ Object
-
#group_by ⇒ Object
This method was borrowed from ActiveSupport code.
-
#sum(identity = 0, &block) ⇒ Object
File activesupport/lib/active_support/core_ext/enumerable.rb, line 57.
- #uniq_by ⇒ Object
Instance Method Details
#count_by ⇒ Object
2 3 4 5 6 7 8 9 |
# File 'lib/rubyc/core_extensions.rb', line 2 def count_by self.inject({}) do |memo, elem| key = yield elem memo[key] ||= 0 memo[key] += 1 memo end end |
#group_by ⇒ Object
This method was borrowed from ActiveSupport code
12 13 14 15 16 17 18 19 |
# File 'lib/rubyc/core_extensions.rb', line 12 def group_by self.inject({}) do |memo, elem| key = yield elem memo[key] ||= [] memo[key] << elem memo end end |
#sum(identity = 0, &block) ⇒ Object
File activesupport/lib/active_support/core_ext/enumerable.rb, line 57
22 23 24 25 26 27 28 |
# File 'lib/rubyc/core_extensions.rb', line 22 def sum(identity = 0, &block) if block_given? map(&block).sum(identity) else inject { |sum, element| sum + element } || identity end end |
#uniq_by ⇒ Object
30 31 32 33 34 35 |
# File 'lib/rubyc/core_extensions.rb', line 30 def uniq_by each_with_object({}) do |obj, memo| val, data = yield obj memo[val] ||= data end.values end |