diff options
author | Kevin Deisz <kevin.deisz@gmail.com> | 2015-05-28 20:40:27 -0400 |
---|---|---|
committer | Kevin Deisz <kevin.deisz@gmail.com> | 2015-05-28 20:41:03 -0400 |
commit | d909211736b1e85cb011a1a3a87c08d1d1f32a63 (patch) | |
tree | 093ff74c6cddca72964fed20f47542dad37a9ff4 /activesupport/lib/active_support | |
parent | 2f9d88954caf4fd75098e813a3ab8bf50ef6ace4 (diff) | |
download | rails-d909211736b1e85cb011a1a3a87c08d1d1f32a63.tar.gz rails-d909211736b1e85cb011a1a3a87c08d1d1f32a63.tar.bz2 rails-d909211736b1e85cb011a1a3a87c08d1d1f32a63.zip |
Add Enumerable#pluck.
Allows fetching the same values from arrays as from ActiveRecord associations.
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/core_ext/enumerable.rb | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 7a893292b3..d28f26260e 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -71,6 +71,14 @@ module Enumerable def without(*elements) reject { |element| elements.include?(element) } end + + # Convert an enumerable to an array based on the given key. + # + # [{ name: "David" }, { name: "Rafael" }, { name: "Aaron" }].pluck(:name) + # => ["David", "Rafael", "Aaron"] + def pluck(key) + map { |element| element[key] } + end end class Range #:nodoc: |