aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2018-05-21 17:46:48 +0200
committerGitHub <noreply@github.com>2018-05-21 17:46:48 +0200
commit41147e3ef802fe7c6aa0eafc9e584fd68f05d395 (patch)
tree8473f496ee321f83028608767488f238324fa8b8 /guides/source
parentdb485a8d992ab4eda513bfe758ebc5478aec661c (diff)
parent429f15ff7ffbd3f8d1647e5a337daa939f10944b (diff)
downloadrails-41147e3ef802fe7c6aa0eafc9e584fd68f05d395.tar.gz
rails-41147e3ef802fe7c6aa0eafc9e584fd68f05d395.tar.bz2
rails-41147e3ef802fe7c6aa0eafc9e584fd68f05d395.zip
Merge pull request #32523 from kaspth/enumerable-index-with-extension
Add Enumerable#index_with.
Diffstat (limited to 'guides/source')
-rw-r--r--guides/source/active_support_core_extensions.md15
1 files changed, 15 insertions, 0 deletions
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`: