Groovy syntax explanation / resources

I started practicing in groovy yesterday. Here is an example on the groovy website that I understand, but I would like to know more about why it works the way it does. I am confused by who[1..-1] . This is how to say who[1..who.length()-1] ? I can not find documentation for this syntax. Are there any good groovy study guides other than those at http://groovy.codehaus.org/ ?

 class Greet { def name Greet(who) { name = who[0].toUpperCase() + who[1..-1] } def salute() { println "Hello $name!" } } g = new Greet('world') // create object g.salute() // Output "Hello World!" 
+4
source share
2 answers

You are right - a negative number in a range mainly refers to the end of the list, not to the beginning. -x equivalent to who.length()-x .

What you are dealing with is known as fragments in Python. (I mention terminology because searching for something like “groovy slices” can help you find more information, although I don’t know if they are really called “slices” in relation to Groovy.) You can find more information about this syntax feature here .

For other resources, I found Groovy's book in action quite useful for learning Groovy.

+6
source

For several languages, PLEAC is an excellent resource.

0
source

All Articles