scan returning matchdata

T

trans. (T. Onoma)

Wouldn't it be more useful if String#scan returned an array of MatchData?
Assuming that back compatibility should be preserved here, could we at least
get an alternate method that does so, say #mscan ?

Thanks,
T.
 
D

David A. Black

Hi --

Wouldn't it be more useful if String#scan returned an array of MatchData?

I don't think so; most of the time, for me anyway, the array elements
are directly useful, and if they were returned embedded in a MatchData
object, I'd just end up having to dig them out.
Assuming that back compatibility should be preserved here, could we at least
get an alternate method that does so, say #mscan ?

It's pretty easy:

def mscan(str,re)
m = []
str.scan(re) { m << $~ }
m
end


David
 
T

trans. (T. Onoma)

On Wednesday 10 November 2004 06:09 am, David A. Black wrote:
| Hi --
|
| On Wed, 10 Nov 2004, trans. (T. Onoma) wrote:
| > Wouldn't it be more useful if String#scan returned an array of MatchData?
|
| I don't think so; most of the time, for me anyway, the array elements
| are directly useful, and if they were returned embedded in a MatchData
| object, I'd just end up having to dig them out.

Right. The only caveat being that an "mscan" can do thinks #scan cannot. So
using it would be better even if it meant slightly more "digging" in common
cases. But having a separate method works just as well for me...

| > Assuming that back compatibility should be preserved here, could we at
| > least get an alternate method that does so, say #mscan ?
|
| It's pretty easy:
|
| def mscan(str,re)
| m = []
| str.scan(re) { m << $~ }
| m
| end
|
|

Bless you, David! Worked like a charm. I had no idea one could do it this way
--quite a relief.

T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,891
Members
47,423
Latest member
henerygril

Latest Threads

Top