Enumerables

.all? {|obj| block } -> true or false

Passes each element of an array (or collection) to the block.  Returns true if the block never returns false or nil.

%w[ant bear cat].all? { |word| word.length >= 3 } #=> true
%w[ant bear cat].all? { |word| word.length >= 4 } #=> false
[nil, true, 99].all?                              #=> false

 

.any? {|obj| block } -> true or false

Passes each element of an array (or collection) to the block.  Returns true if the block ever returns false or nil.

%w[ant bear cat].any? { |word| word.length >= 3 } #=> true
%w[ant bear cat].any? { |word| word.length >= 4 } #=> true
[nil, true, 99].any?                              #=> true

 

.map / .collect {|obj| block } -> array

Returns a new array with the results of each element in the enum.  If no block is given, returns an enumerator.

(1..4).map { |i| i*i }      #=> [1, 4, 9, 16]
(1..4).collect { "cat"  }   #=> ["cat", "cat", "cat", "cat"]

 

.max / max(n) {|a, b| block } -> object

Returns the object with the maximum value.  When an argument is given, returns maximum n elements.

The first form assumes all objects implement Comparable; the second uses the block to return a <=> b.

a = %w(albatross dog horse)
a.max                                     #=> "horse"
a.max { |a, b| a.length <=> b.length }    #=> "albatross"
a.max(2)                                  #=> ["horse", "dog"]
a.max(2) {|a, b| a.length <=> b.length }  #=> ["albatross", "horse"]

 

.max_by / max_by(n) {|a, b| block } -> object

Returns the object with the maximum value from the given block.  When no block is given, returns an enumerator instead.

a = %w(albatross dog horse)
a.max_by { |x| x.length }   #=> "albatross"
a.max_by(2) {|x| x.length } #=> ["albatross", "horse"]

 

.min / min(n) {|a, b| block } -> object

Returns the object with the minimum value.  When an argument is given, returns minimum n elements.

a = %w(albatross dog horse)
a.min                                   #=> "albatross"
a.min { |a, b| a.length <=> b.length }  #=> "dog"a.min(2)                                  #=> ["albatross", "dog"]
a.min(2) {|a, b| a.length <=> b.length }  #=> ["dog", "horse"]

 

.min_by / min_by(n) {|a, b| block } -> object

Returns the object with the minimum value from the given block.  When no block is given, returns an enumerator instead.

a = %w(albatross dog horse)
a.min_by { |x| x.length }   #=> "dog"
p a.min_by(2) {|x| x.length } #=> ["dog", "horse"]

 

.minmax / minmax { |a, b| block } -> [min, max] 

a = %w(albatross dog horse)
a.minmax                                  #=> ["albatross", "horse"]
a.minmax { |a, b| a.length <=> b.length } #=> ["dog", "albatross"]
a.minmax_by { |x| x.length }   #=> ["dog", "albatross"]

 

.include?(obj) -> true or false

Returns true if any members of enum is equal to object.

"hello".include?("e")         #=> true
"hello".include?("a")         #=> false

 

.group_by { |obj| block } -> hash

Returns a hash where the keys are the evaluated results from the block and the values are arrays of elements in the collection that correspond to the keys. If no block is given, returns an enumerator.

(1..6).group_by { |i| i%3 }   #=> {0=>[3, 6], 1=>[1, 4], 2=>[2, 5]}

 

.select { |obj| block } -> array 

Returns an array containing all the elements in the enum for which the given block evaluates true.

[1,2,3,4,5].select { |num|  num.even?  }   #=> [2, 4]

 

.sort { |obj| block } -> array 

Returns an array containing all the items sorted by the results of the block.

%w(rhea kea flea).sort          #=> ["flea", "kea", "rhea"]
(1..10).sort { |a, b| b <=> a }  #=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

 

.sort_by { |obj| block } -> array 

Sorts enum using a set of keys generated by mapping the values in enum through the given block.

%w{apple fig}.sort_by {|word| word.length} #=> ["pear", "apple"]
Advertisements
Enumerables

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s