Module: Enumerable
- Defined in:
- lib/everythingrb/enumerable.rb
Overview
Extensions to Ruby’s core Enumerable module
Provides:
-
#join_map: Combine filter_map and join operations into one step
-
#group_by_key: Group elements by key or nested keys, simplifying collection organization
Instance Method Summary collapse
-
#group_by_key(*keys) {|value| ... } ⇒ Hash, Enumerator
Groups elements by a given key or nested keys.
-
#join_map(join_with = "", with_index: false) {|element, index| ... } ⇒ String, Enumerator
Combines filter_map and join operations.
Instance Method Details
#group_by_key(*keys) {|value| ... } ⇒ Hash, Enumerator
Groups elements by a given key or nested keys
89 90 91 92 93 94 95 |
# File 'lib/everythingrb/enumerable.rb', line 89 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, Enumerator
Combines filter_map and join operations
41 42 43 44 45 46 47 48 49 |
# File 'lib/everythingrb/enumerable.rb', line 41 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 |