From: Jason Creighton Date: 2003-08-26T13:06:23+09:00 Subject: Re: Extra Array methods? On Tue, 26 Aug 2003 03:17:32 +0900 mgarriss wrote: > I have been using a little extension of Array that I wrote for awhile: > > class Array > def and?() each{|e| return false unless yield e }; true end > def or?() each{|e| return true if yield e }; false end > end > Then I discovered Array#detect because of a post by Jason Creighton, he > used it in his WrapHash class (btw, nice class Jason). Why thank you. > So I run to irb > and played with #detect. It's cool and I'll be using it but it's not a > replacement for my #and? and #or? methods. I still have to mess with > Array myself. Okay, any? and all? were mentioned in other posts but 'ri' is your friend. The 1.8 version is on rdoc.sf.net, go to the download page and scroll down to the bottom. Here's what you can do with it: ~$ ri Enumerable This is a test 'ri'. Please report errors and omissions on http://www.rubygarden.org/ruby?RIOnePointEight ------------------------------------------------------------------------ module: Enumerable ------------------------------------------------------------------------ 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. ------------------------------------------------------------------------ all?, any?, collect, detect, each_with_index, entries, find, find_all, grep, include?, inject, map, max, member?, min, partition, reject, select, sort, sort_by, to_a, zip ------------------------------------------------------------------------ # any? is a unique method so we don't have to say "Enumerable#any?" ~$ ri any? This is a test 'ri'. Please report errors and omissions on http://www.rubygarden.org/ruby?RIOnePointEight -------------------------------------------------------- Enumerable#any? enumObj.any? [{| obj | block } ] -> true or false ------------------------------------------------------------------------ Passes each element of the collection to the given block. The method returns true if the block ever returns a value other that false or nil. If the block is not given, Ruby adds an implicit block of {|obj| obj} (that is any? will return true if at least one of the collection members is not 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 ~$ ri collect This is a test 'ri'. Please report errors and omissions on http://www.rubygarden.org/ruby?RIOnePointEight The method named `collect' is not unique among Ruby's classes and modules: Array#collect, Enumerable#collect # Oops, Array implements its own #collect, we have to be more specific ~$ ri Enumerable#collect This is a test 'ri'. Please report errors and omissions on http://www.rubygarden.org/ruby?RIOnePointEight ----------------------------------------------------- Enumerable#collect enumObj.collect {| obj | block } -> anArray ------------------------------------------------------------------------ Returns a new array with the results of running block once for every element in enumObj. (1..4).collect {|i| i*i } #=> [1, 4, 9, 16] (1..4).collect { "cat" } #=> ["cat", "cat", "cat", "cat"] This incredibly handy tool is, of course, by Dave Thomas. Jason Creighton