aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKasper Timm Hansen <kaspth@gmail.com>2018-05-21 17:44:13 +0200
committerKasper Timm Hansen <kaspth@gmail.com>2018-05-21 17:46:05 +0200
commit429f15ff7ffbd3f8d1647e5a337daa939f10944b (patch)
tree995e38db3b8224737fb3d7f2481bab3467580fc0
parent39c22303a64db5592a1df6ece3d775ce09ff463c (diff)
downloadrails-429f15ff7ffbd3f8d1647e5a337daa939f10944b.tar.gz
rails-429f15ff7ffbd3f8d1647e5a337daa939f10944b.tar.bz2
rails-429f15ff7ffbd3f8d1647e5a337daa939f10944b.zip
[ci skip] Add documentation/changelog entry.
-rw-r--r--activesupport/CHANGELOG.md15
-rw-r--r--guides/source/active_support_core_extensions.md15
2 files changed, 30 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 62c0f612a8..a384fc3bd2 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,3 +1,18 @@
+* Add `index_with` to Enumerable.
+
+ Allows creating a hash from an enumerable with the value from a passed block
+ or a default argument.
+
+ %i( title body ).index_with { |attr| post.public_send(attr) }
+ # => { title: "hey", body: "what's up?" }
+
+ %i( title body ).index_with(nil)
+ # => { title: nil, body: nil }
+
+ Closely linked with its brethen `index_by`.
+
+ *Kasper Timm Hansen*
+
* Fix bug where `ActiveSupport::Timezone.all` would fail when tzinfo data for
any timezone defined in `ActiveSupport::TimeZone::MAPPING` is missing.
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`: