Reputation: 2617
I started dabbling in groovy yesterday. There's an example on the groovy website that I understand but I would like to know more about why it works the way it does. What's confusing me is who[1..-1]
. Is this like saying who[1..who.length()-1]
? I can't find any documentation on this syntax.
Are there any good groovy tutorials out there besides what is on 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!"
Upvotes: 4
Views: 2146
Reputation: 411182
You're right -- a negative number in a range basically refers to the end of the list, rather than the beginning. -x
is equivalent to who.length()-x
.
What you're dealing with is known as slices in Python. (I mention the terminology because searching for something like "groovy slices" may help you find more information, although I don't know if they're actually called "slices" in reference to Groovy.) You can find more information on this particular syntax feature here.
As for other resources, I found the book Groovy in Action to be quite handy for learning Groovy.
Upvotes: 6