Relies on: <=>
The Enumerable
mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method each
, which yields successive members of the collection. If Enumerable#max
, #min
, or #sort
is used, the objects in the collection must also implement a meaningful <=>
operator, as these methods rely on an ordering between members of the collection.
(1..4).collect {|i| i*i } → [1, 4, 9, 16]
(1..4).collect { "cat" } → ["cat", "cat", "cat", "cat"]
nil
false
. Returns nil
if no object matches.
(1..10).detect {|i| i % 5 == 0 and i % 7 == 0 } → nil
(1..100).detect {|i| i % 5 == 0 and i % 7 == 0 } → 35
nil
hash = Hash.new
%w(cat dog wombat).each_with_index {|item, index|
hash[item] = index
}
hash → {"cat"=>0, "wombat"=>2, "dog"=>1}
Enumerable#to_a
.
nil
Enumerable#detect
.
false
(see also Enumerable#reject
).
(1..10).find_all {|i| i % 3 == 0 } → [3, 6, 9]
Pattern === element
. If the optional block is
supplied, each matching element is passed to it, and the block's
result is stored in the output array.
(1..100).grep 38..44 → [38, 39, 40, 41, 42, 43, 44]
c = IO.constants
c.grep(/SEEK/) → ["SEEK_END", "SEEK_CUR", "SEEK_SET"]
res = c.grep(/SEEK/) {|v| IO.const_get(v) }
res → [2, 1, 0]
true
or false
true
if any member of enumObj equals
anObject. Equality is tested using ==
.
IO.constants.include? "SEEK_SET" → true
IO.constants.include? "SEEK_NO_FURTHER" → false
Enumerable#collect
.
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"
true
or false
Enumerable#include?
.
Comparable
; the second
uses the block to return a <=> b.
a = %w(albatross dog horse)
a.min → "albatross"
a.min {|a,b| a.length <=> b.length } → "dog"
false
(see also Enumerable#find_all
).
(1..10).reject {|i| i % 3 == 0 } → [1, 2, 4, 5, 7, 8, 10]
Enumerable#find_all
.
<=>
method, or by using the
results of the supplied block. The block should return -1, 0, or
+1 depending on the comparison between a and b.
%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]
files = Dir["*"]
sorted = files.sort {|a,b| File.new(a).mtime <=> File.new(b).mtime}
sorted → ["mon", "tues", "wed", "thurs"]
File
objects
during every comparison. A slightly better technique is to use
the Kernel#test
method to generate the modification times
directly.
files = Dir["*"]
sorted = files.sort { |a,b|
test(?M, a) <=> test(?M, b)
}
sorted → ["mon", "tues", "wed", "thurs"]
Time
objects.
A more efficient technique is to cache
the sort keys (modification times in this case) before the sort.
Perl users often call this approach a Schwartzian Transform,
after Randal Schwartz. We construct a temporary array,
where each element is an array containing our sort key along
with the filename. We sort this array, and then extract the
filename from the result.
sorted = Dir["*"].collect { |f|
[test(?M, f), f]
}.sort.collect { |f| f[1] }
sorted → ["mon", "tues", "wed", "thurs"]
(1..7).to_a → [1, 2, 3, 4, 5, 6, 7]
{ 'a'=>1, 'b'=>2, 'c'=>3 }.to_a → [["a", 1], ["b", 2], ["c", 3]]
Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Copyright © 2001 by Addison Wesley Longman, Inc. This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/).
Distribution of substantively modified versions of this document is prohibited without the explicit permission of the copyright holder.
Distribution of the work or derivative of the work in any standard (paper) book form is prohibited unless prior permission is obtained from the copyright holder.