Module: Enumerable
- Defined in:
- lib/rubyc/cli.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.
Instance Method Details
#count_by ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/rubyc/cli.rb', line 6 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
16 17 18 19 20 21 22 23 |
# File 'lib/rubyc/cli.rb', line 16 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
26 27 28 29 30 31 32 |
# File 'lib/rubyc/cli.rb', line 26 def sum(identity = 0, &block) if block_given? map(&block).sum(identity) else inject { |sum, element| sum + element } || identity end end |