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

**URL:** <https://www.ruby-forum.com/t/matchdata-captures-expected-two-items-but-got-one/184406>\
**Category:** Ruby\
**Created:** [March 2, 2010, 2:15am UTC](https://www.ruby-forum.com/t/matchdata-captures-expected-two-items-but-got-one/184406 "2010-03-02T02:15:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2010, 2:15am UTC](https://www.ruby-forum.com/t/matchdata-captures-expected-two-items-but-got-one/184406/1 "2010-03-02T02:15:32Z")

</div>

Looking at the MatchData docs:  
[http://ruby-doc.org/core/classes/MatchData.html#M001278](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](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

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2010, 2:37am UTC](https://www.ruby-forum.com/t/matchdata-captures-expected-two-items-but-got-one/184406/2 "2010-03-02T02:37:52Z")

</div>

On Mon, Mar 1, 2010 at 7:15 PM, Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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/](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”]

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2010, 5:48am UTC](https://www.ruby-forum.com/t/matchdata-captures-expected-two-items-but-got-one/184406/3 "2010-03-02T05:48:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2010, 6:58am UTC](https://www.ruby-forum.com/t/matchdata-captures-expected-two-items-but-got-one/184406/4 "2010-03-02T06:58:20Z")

</div>

On Tue, Mar 2, 2010 at 12:48 PM, Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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”]

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2010, 12:42pm UTC](https://www.ruby-forum.com/t/matchdata-captures-expected-two-items-but-got-one/184406/5 "2010-03-02T12:42:32Z")

</div>

2010/3/2 Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2010, 4:33pm UTC](https://www.ruby-forum.com/t/matchdata-captures-expected-two-items-but-got-one/184406/6 "2010-03-02T16:33:54Z")

</div>

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](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 [class String - RDoc Documentation](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

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2010, 5:48pm UTC](https://www.ruby-forum.com/t/matchdata-captures-expected-two-items-but-got-one/184406/7 "2010-03-02T17:48:05Z")

</div>

2010/3/2 Sarah A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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 [class String - RDoc Documentation](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

---

<div class="post-metadata">

**Author:** ![sackerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/59ef9b/32.png) [@sackerson](https://www.ruby-forum.com/u/sackerson)\
**Post date:** [March 2, 2010, 7:11pm UTC](https://www.ruby-forum.com/t/matchdata-captures-expected-two-items-but-got-one/184406/8 "2010-03-02T19:11:12Z")

</div>

> 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
