Say I wanted to see if an array contained a number or not. Would I use
the match method?
E.g.
x=["a","b",2, "c"]
puts("This array contains numbers") if x.match(/\d/)
I'm a real beginner at ruby, so simple explanations for simple folk
please. I appreciate your help.
Nice try, but match matches strings and you're wanting numbers.
Note that the Array class includes the Enumerable module, so all methods in Enumerable are available in Array. Enumerable has a method called "any?" that we can use. Here's what ri says about Enumerable#any?
$ ri Enumerable#any?
-------------------------------------------------------- Enumerable#any?
enum.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 than
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.
1. The Enumerable#grep method uses the "matching" operator === to search for all matches to the argument, which are collected into a new array.
2. In this case, grep's argument is a class, so each element of x is matched using the following condition: Numeric === the_element.
3. When called on classes, === checks if the given object is a member of the class (or a descendent) or not.
But this is not the most efficient solution since it has to pass over the entire collection (instead of stopping on the first match), and it makes a new array with the results. It's really only useful when you need all matches, for some reason. Otherwise, follow the suggestion to use #any? and #is_a? or #kind_of?.
References:
ri Module#===
ri Enumerable#grep
···
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
Although I wonder what your opinion (Tim) is on is_a? vs ===.
My opinion carries no weight whatsoever, but fwiw, I always use is_a? or kind_of? because it's not obvious what === does. Also I can't ever remember which order the arguments go
Also I can't ever
remember which order the arguments [of ===] go
Try to remember it like this: The one that defines the behaviour of === (i.e.
the class, range or regexp) goes first.
But yeah, I don't usually explicitly use === either.
I would say that === is more appropriate in the case where you are
generalizing and the thing you might match against will vary. This is
in fact what grep does - it will work on anything that responds to
===.
For example, it would be useful in the case of a find version of grep
(to solve the above problem with grep iterating over the whole array).
Let's call it grep_first:
class Array ; def grep_first pat ; find { | el | pat === el } ; end ;
end
array = ['foo','bar',2,'7','hi' ]
array.grep_first Numeric # => 2
array.grep_first /\d/ # => '7'
array.grep_first 'bar' # => 'bar'
array.grep_first 'foobar' # => nil
But if you specifically _know_ you are matching a type then I think
is_a or kind_of is probably better. Interested in others' take on
this.
Regards,
Dan
···
On Jan 3, 11:58 pm, Sebastian Hungerecker <sep...@googlemail.com> wrote:
But yeah, I don't usually explicitly use === either.