MatchData captures... expected two items, but got one?

S

Sarah Allen

Looking at the MatchData docs:
http://ruby-doc.org/core/classes/MatchData.html#M001278

It looks like I should be able to get an array from .captures with every
match. I swear I've done this before, but I was teaching a class today
and couldn't figure out what's going on in this simple example... maybe
I typed something wrong and just need another pair of eyes.


$ irb=> ["word1"]

testing the same string and expression in http://www.rubular.com yields
two matches

$ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin9.7.0]

Can anyone give me a clue?

Thanks in advance,
Sarah
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Looking at the MatchData docs:
http://ruby-doc.org/core/classes/MatchData.html#M001278

It looks like I should be able to get an array from .captures with every
match. I swear I've done this before, but I was teaching a class today
and couldn't figure out what's going on in this simple example... maybe
I typed something wrong and just need another pair of eyes.


$ irb=> ["word1"]

testing the same string and expression in http://www.rubular.com yields
two matches

$ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin9.7.0]

Can anyone give me a clue?

Thanks in advance,
Sarah

test = "word1 foo word2"
m = test.scan(/word./)
m # => ["word1", "word2"]
 
S

Sarah Allen

Josh said:
Not sure what the answer is with match, I usually use scan like this:

test = "word1 foo word2"
m = test.scan(/word./)
m # => ["word1", "word2"]

Thanks Josh. I should have mentioned that I found that work-around. I
just need to be able to explain why match doesn't seem to work the way
the documentation says it should -- at least by my reading of it.

Best,
Sarah
 
R

Robert Klemme

2010/3/2 Sarah Allen said:
Josh said:
Not sure what the answer is with match, I usually use scan like this:

test =3D "word1 foo word2"
m =3D test.scan(/word./)
m # =3D> ["word1", "word2"]

Thanks Josh. =A0I should have mentioned that I found that work-around. = =A0I
just need to be able to explain why match doesn't seem to work the way
the documentation says it should -- at least by my reading of it.

Where exactly is the difference between the functionality and the descripti=
on?

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
S

Sarah Allen

Robert said:
Where exactly is the difference between the functionality and the
description?

Kind regards

robert

The docs are correct, but not particularly helpful in their description.
It is not ideal to use a word in its definition:

captures: "Returns the array of captures"
http://ruby-doc.org/core/classes/MatchData.html#M001278

If I had looked closer at the example I might have figured it out, since
upon reflection it is pretty standard regex. My attempts at debugging
with rubular further confused me since it reports two matches from the
single capture expression. botp pointed out that I needed to specify
one capture per result. Which the ruby-doc example also illustrates:

f1,f2,f3,f4 = /(.)(.)(\d+)(\d)/.match("THX1138.").captures
f1 #=> "H"
f2 #=> "X"
f3 #=> "113"
f4 #=> "8"

I was confused by the rubular experiment and also by the String.scan
docs http://ruby-doc.org/core/classes/String.html#M000812 where captures
are described as "groups"... or maybe they were just referring to the
result of evaluating the capture with a scan.

Thanks everyone for your help!

Sarah
 
R

Robert Klemme

The docs are correct, but not particularly helpful in their description.

Ah, OK.
I was confused by the rubular experiment and also by the String.scan
docs http://ruby-doc.org/core/classes/String.html#M000812 where captures
are described as "groups"... or maybe they were just referring to the
result of evaluating the capture with a scan.

Whenever I need to find out things like these I test with IRB. That
works pretty well for me. Maybe it's an option for you, too.
Thanks everyone for your help!

You're welcome!

Kind regards

robert
 
R

Roger Pack

Thanks Josh. I should have mentioned that I found that work-around. I
just need to be able to explain why match doesn't seem to work the way
the documentation says it should -- at least by my reading of it.

Yeah, appears match only matches the first match. Sounds like a forest
fire!
-rp
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top