A newbie question

Hi,

I am trying to create an array from another array using a regular
expression to compare them. If an element match the regular expression,
then I want it to appears in my new array, otherwise it can be
discarded. I have come up with the following code (for example, getting
strings than end in ‘abba’ in an array of string)

new_array = (array.collect { |x| if( x =~ /abba$/ ) then x else nil end
}).compact

It works fine, but I want to know if there’s is a more simple way to do
this since I do this a lot in my scripts.

Thankx,
Ben.

Use Array#grep

%w{ cat dog mouse zebra rhino }
=> [“cat”, “dog”, “mouse”, “zebra”, “rhino”]
%w{ cat dog mouse zebra rhino }.grep(/e/)
=> [“mouse”, “zebra”]

···

On Wed, Mar 12, 2003 at 08:51:13AM +0900, Ben Thomas wrote:

Hi,

I am trying to create an array from another array using a regular
expression to compare them. If an element match the regular expression,
then I want it to appears in my new array, otherwise it can be
discarded. I have come up with the following code (for example, getting
strings than end in ‘abba’ in an array of string)

new_array = (array.collect { |x| if( x =~ /abba$/ ) then x else nil end
}).compact

It works fine, but I want to know if there’s is a more simple way to do
this since I do this a lot in my scripts.

Thankx,
Ben.


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

Use Array#grep

That’s what I’ve been missing in Ruby!!! Thank you very much, Daniel (and
Matz, of course). I just verified that Enumerable#grep simply invokes method
“===” on its parameter for every element, and that’s exactly what I usually
need. And with an optional block you get “selective collect” functionality.
This is so great!!!

Gennady.

···

----- Original Message -----
From: “Daniel Carrera” dcarrera@math.umd.edu
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, March 11, 2003 3:56 PM
Subject: Re: a newbie question

%w{ cat dog mouse zebra rhino }
=> [“cat”, “dog”, “mouse”, “zebra”, “rhino”]
%w{ cat dog mouse zebra rhino }.grep(/e/)
=> [“mouse”, “zebra”]

On Wed, Mar 12, 2003 at 08:51:13AM +0900, Ben Thomas wrote:

Hi,

I am trying to create an array from another array using a regular
expression to compare them. If an element match the regular expression,
then I want it to appears in my new array, otherwise it can be
discarded. I have come up with the following code (for example, getting
strings than end in ‘abba’ in an array of string)

new_array = (array.collect { |x| if( x =~ /abba$/ ) then x else nil end
}).compact

It works fine, but I want to know if there’s is a more simple way to do
this since I do this a lot in my scripts.

Thankx,
Ben.


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

Gennady wrote:

That’s what I’ve been missing in Ruby!!! Thank you very much, Daniel (and
Matz, of course). I just verified that Enumerable#grep simply invokes method
“===” on its parameter for every element, and that’s exactly what I usually
need. And with an optional block you get “selective collect” functionality.
This is so great!!!

=== is one of the unsung heroes of ruby. It’s a protocol that unifies
grep and case into a general notion of pattern matching. Here’s one use
I like:

[1,2,“345”,“678”].grep String
[“345”, “678”]

Thankx all for the answers. Think I’m gonna read on === since its
something I don’t even use in my ruby script :slight_smile:

Ben.

Joel VanderWerf wrote:

···

Gennady wrote:

That’s what I’ve been missing in Ruby!!! Thank you very much, Daniel (and
Matz, of course). I just verified that Enumerable#grep simply invokes
method
“===” on its parameter for every element, and that’s exactly what I
usually
need. And with an optional block you get “selective collect”
functionality.
This is so great!!!

=== is one of the unsung heroes of ruby. It’s a protocol that unifies
grep and case into a general notion of pattern matching. Here’s one
use I like:

[1,2,“345”,“678”].grep String
[“345”, “678”]