Module: Enumerable
- Defined in:
- lib/everythingrb/core/enumerable.rb
Overview
Extensions to Ruby’s core Enumerable module
These additions make working with any enumerable collection more expressive by combining common operations into convenient methods.
Instance Method Summary collapse
-
#group_by_key(*keys) {|value| ... } ⇒ Hash
Groups elements by a given key or nested keys.
-
#join_map(join_with = "", with_index: false) {|element, index| ... } ⇒ String
Combines filter_map and join operations.
Instance Method Details
#group_by_key(*keys) {|value| ... } ⇒ Hash
Groups elements by a given key or nested keys
85 86 87 88 89 90 91 |
# File 'lib/everythingrb/core/enumerable.rb', line 85 def group_by_key(*keys, &block) group_by do |value| result = value.respond_to?(:dig) ? value.dig(*keys) : nil result = yield(result) if block result end end |
#join_map(join_with = "", with_index: false) {|element, index| ... } ⇒ String
Combines filter_map and join operations
38 39 40 41 42 43 44 45 46 |
# File 'lib/everythingrb/core/enumerable.rb', line 38 def join_map(join_with = "", with_index: false, &block) block = ->(i) { i } if block.nil? if with_index filter_map.with_index(&block).join(join_with) else filter_map(&block).join(join_with) end end |