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

···

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

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

···

On Mon, Mar 1, 2010 at 7:15 PM, Sarah Allen <sarah@ultrasaurus.com> wrote:

Looking at the MatchData docs:
class MatchData - RDoc Documentation

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
--
Posted via http://www.ruby-forum.com/\.

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

Josh Cheek 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

···

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

..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"]

···

On Tue, Mar 2, 2010 at 12:48 PM, Sarah Allen <sarah@ultrasaurus.com> wrote:

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

Kind regards

robert

···

2010/3/2 Sarah Allen <sarah@ultrasaurus.com>:

Josh Cheek 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.

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

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

···

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

Robert Klemme 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 class String - RDoc Documentation 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

···

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

Robert Klemme 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 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

···

2010/3/2 Sarah Allen <sarah@ultrasaurus.com>:

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