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