From 429f15ff7ffbd3f8d1647e5a337daa939f10944b Mon Sep 17 00:00:00 2001 From: Kasper Timm Hansen Date: Mon, 21 May 2018 17:44:13 +0200 Subject: [ci skip] Add documentation/changelog entry. --- guides/source/active_support_core_extensions.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'guides') diff --git a/guides/source/active_support_core_extensions.md b/guides/source/active_support_core_extensions.md index ae2e1faf14..cde217e1e4 100644 --- a/guides/source/active_support_core_extensions.md +++ b/guides/source/active_support_core_extensions.md @@ -2039,6 +2039,21 @@ WARNING. Keys should normally be unique. If the block returns the same value for NOTE: Defined in `active_support/core_ext/enumerable.rb`. +### `index_with` + +The method `index_with` generates a hash with the elements of an enumerable as keys. The value +is either a passed default or returned in a block. + +```ruby +%i( title body created_at ).index_with { |attr_name| public_send(attr_name) } +# => { title: "hey", body: "what's up?", … } + +WEEKDAYS.index_with([ Interval.all_day ]) +# => { monday: [ 0, 1440 ], … } +``` + +NOTE: Defined in `active_support/core_ext/enumerable.rb`. + ### `many?` The method `many?` is shorthand for `collection.size > 1`: -- cgit v1.2.3