MatchData providing unexpected result

Hi,

Below are two tests of using MatchData. The first is essentially Hal
Futon’s taken from The Ruby Way, 2nd. ed. [Thanks, Hal]. In
particular, m[0] returns the string being searched.

The 2nd is my humble use. For mine, m[0] returns the search pattern,
it seems,

I can’t anything in the code to account for this difference. I’m
expecting the first kind of response in a Rails app I’m working on,
but I’m getting the second kind of response.

Any ideas?

Thanks in Advance,
Richard

BTW, I running Ruby 1.8.6 over WindowsXP-Pro.SP3

========= Test =========
puts “\n1.”
pat = /(.+[aiu])(.+[aiu])(.+[aiu])(.+[aiu])/i
str = “Fujiyama”
m = pat.match(str)
(0…5).to_a.each { |i| puts “m[%d]: %s” % [i, m[i].to_s] }

puts “\n2.”
pat = /noun/i
str = “The ‘noun’ word is here”
m = pat.match(str)
(0…5).to_a.each { |i| puts “m[%d] %s” % [i, m[i].to_s] }

On Sun, Feb 28, 2010 at 12:16 PM, RichardOnRails
[email protected] wrote:

expecting the first kind of response in a Rails app I’m working on,
but I’m getting the second kind of response.

Any ideas?

m[0] doesn’t return the string being searched, it returns everything
which the entire pattern matched.

In the first case /(.+[aiu])(.+[aiu])(.+[aiu])(.+[aiu])/i entirely
matches “Fujiyama”

in the second case /noun/i only matches a portion of the string, so
that’s what is the value of m[0]


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

Hi Rick,

m[0] doesn’t return the string being searched, it returns everything
which the entire pattern matched.

I got it! In Hal’s example, the matched string just happened to be
the entire string. I didn’t realize that distinction, but I’m on
board now.

Thanks again for upteenth time for pulling my fat out of the fire.

Best wishes,
Richard

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