18 Oct 2008, 06:49
Generic-user-small

Robert Kuhar (11 posts)

I was playing with Chapter 6’s RegEx.groovy example and wanted to iterate over matcher. My first attempt ~~~ matcher.each { println “$it” } ~~~ blew up with ~~~ …MissingMethodException…No signature of method…is applicable for argument types: (java.lang.String, java.lang.String) values: {“Groovy”, “G”} ~~~

Decoding the exception wasn’t a problem and my solution worked when I changed it to ~~~ matcher.each { p1, p2 -> println “$p1 $p2” } ~~~

I have 2 questions:

  1. Where do I go to find out the .each of a Matcher takes 2 arguments for its closure? I was hoping to find this information at html/groovy-jdk/java/util/regex/Matcher.html#each(groovy.lang.Closure closure) but it pretty much just stated the obvious. Are there any better docs on this stuff than the distributed JavaDocs?

  2. If I move my matcher.each below the book’s “println “with elements ${matcher[0]}…” my iteration produces no output, as if the “with elements” println consumed the matcher or something. Can you explain what I am observing? My script is as follows:

/*
 * The =~ operator returns a matcher object (instanceof j.u.r.Matcher).  If the match results
 * in multiple matches, then the matcher contains an array of matches.
 */
matcher = 'Groovy is groovy' =~ /(G|g)roovy/
// this matcher.each works like a champ
matcher.each { p1, p2 -> println "$p1 $p2" }
print "Size of matcher is ${matcher.size()}"
println " with elements ${matcher[0]} and ${matcher[1]}"
println ""

/*
 * When I move my matcher.each down here, however, it produces no output.
 * It is as if the preceding matcher accesses consumed the elements?  Whats
 * up with that?
 */
matcher.each { p1, p2 -> println "$p1 $p2" }
println "done"
18 Oct 2008, 14:04
Venkatsubramaniam_pragsmall

Venkat Subramaniam (84 posts)

Bob,

On question 2: Looks like you ran into a bug. What version of Groovy are you using? The code you presented works well in Groovy 1.6-beta-2. However, it does not work in Groovy 1.5.7 or earlier.

Also, matcher.each { println it } works well in Groovy 1.6-beta-2 (and also 1.6-beta-1), but not in Groovy 1.5.x.

So, the my answer for Question 2 would be to upgrade to 1.6-beta-2.

On question 1: I suggest that you send an email to the Groovy mailing list so they can either point you to updated documentation or enhance the documentation if needed.

Hope this helps.

20 Oct 2008, 17:25
Generic-user-small

Robert Kuhar (11 posts)

Thanks for looking into this, Venkat. You are correct, I am running Groovy 1.5.6. While upgrading to the 1.6 beta may make my immediate problem go away, we’re going into production with Groovy as our scripting component and I’d rather not be explaining why we’re shipping with a beta core library. I’ll follow up on the Groovy mailing lists in search of better docs.

Thanks again, Bob

  You must be logged in to comment