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

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

test = “word1 foo word2”
=> “word1 foo word2”

m = test.match /(word.)/
=> #<MatchData “word1” 1:“word1”>

m.captures
=> [“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

On Mon, Mar 1, 2010 at 7:15 PM, Sarah A. [email protected]
wrote:

test = “word1 foo word2”
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin9.7.0]

Can anyone give me a clue?

Thanks in advance,
Sarah

Posted via http://www.ruby-forum.com/.

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”]

Josh C. wrote:

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

On Tue, Mar 2, 2010 at 12:48 PM, Sarah A. [email protected]
wrote:

…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.

your match/regex captured only one…
try eg,

m = test.match /(word.).*?(word.)/
=> #<MatchData “word1 foo word2” 1:“word1” 2:“word2”>

m.captures
=> [“word1”, “word2”]

2010/3/2 Sarah A. [email protected]:

Josh C. wrote:

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.

Where exactly is the difference between the functionality and the
description?

Kind regards

robert

Robert K. wrote:

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

2010/3/2 Sarah A. [email protected]:

Robert K. wrote:

Where exactly is the difference between the functionality and the
description?

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs