Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
|
becki:linux:groovy [2010-03-10 17:37] becki |
becki:linux:groovy [2010-03-15 09:57] (aktuell) becki |
||
|---|---|---|---|
| Zeile 24: | Zeile 24: | ||
| ===== Script Files == | ===== Script Files == | ||
| - | + | ==== Shebang == | |
| - | Shebang: | + | |
| <code groovy> | <code groovy> | ||
| Zeile 31: | Zeile 30: | ||
| println "Hallo Sandy!" | println "Hallo Sandy!" | ||
| </code> | </code> | ||
| + | |||
| + | ==== Command Line Arguments == | ||
| + | |||
| + | <code groovy> | ||
| + | // file: test.groovy | ||
| + | println args[0]+ ' '+ args[1]+ ' '+ args[2]+ ' '+ args.length | ||
| + | </code> | ||
| + | |||
| + | Invoking the script with ''test.groovy one two three'' prints ''one two three 3''. | ||
| + | |||
| + | See also [[groovy>Groovy+CLI]] | ||
| + | |||
| + | ==== Stdin stdout stderr == | ||
| + | |||
| + | FIXME ''System.in.text'' | ||
| + | |||
| + | ==== Behind the Scenes == | ||
| A script automatically creates a class which has the same name as the filename of the script: | A script automatically creates a class which has the same name as the filename of the script: | ||
| Zeile 73: | Zeile 89: | ||
| Note that this would have to be done on every system which wants to run a compiled script. Therefore it is better to create a jar file wich already includes the necessary depcendencies. This is described [[groovy>WrappingGroovyScript|here]]. | Note that this would have to be done on every system which wants to run a compiled script. Therefore it is better to create a jar file wich already includes the necessary depcendencies. This is described [[groovy>WrappingGroovyScript|here]]. | ||
| - | ==== Groovy and Java Web Start == | + | ===== Java Web Start == |
| - | FIXME | + | FIXME Add Groovy specific infos about Java Web Start here. |
| - | ==== Groovy and Apache Pivot == | + | See also [[java#java_web_start|generic Java Web Start]] |
| - | FIXME | + | ===== Apache Pivot == |
| + | |||
| + | FIXME Add Groovy specific infos about Pivot here. http://www.ibm.com/developerworks/xml/tutorials/x-pivottut/ | ||
| + | |||
| + | See also generic [[Apache Pivot]] | ||
| + | |||
| + | ==== Java Web Start == | ||
| + | |||
| + | FIXME Add Groovy / Pivot specific infos about Java Web Start here. | ||
| + | |||
| + | See also [[Apache Pivot#java_web_start|general pivot specific things about Java Web Start]] | ||
| ===== Dynamic Typing == | ===== Dynamic Typing == | ||
| Zeile 94: | Zeile 120: | ||
| nine="nine"; println "nine.class: " + nine.class // -> class java.lang.String | nine="nine"; println "nine.class: " + nine.class // -> class java.lang.String | ||
| </code> | </code> | ||
| - | |||
| - | |||
| ===== Collections: Lists, Ranges and Maps == | ===== Collections: Lists, Ranges and Maps == | ||
| + | ==== Lists == | ||
| <code groovy> | <code groovy> | ||
| Zeile 107: | Zeile 132: | ||
| println myList[3] // => null | println myList[3] // => null | ||
| println myList.size // => 3 | println myList.size // => 3 | ||
| - | println myList.class // => class java.util.ArrayList | + | </code> |
| + | |||
| + | Iterate over the list by calling the ''each()'' method on the list and pass in a closure: | ||
| + | |||
| + | <code groovy> | ||
| + | myList.each{ print "$it " } // => 1976 1969 2010 | ||
| + | myList.eachWithIndex{ v, k -> print "$k:$v " } // => 0:1976 1:1969 2:2010 | ||
| </code> | </code> | ||
| Zeile 113: | Zeile 144: | ||
| <code groovy> | <code groovy> | ||
| + | println myList.class // => class java.util.ArrayList | ||
| println myList.get(0) // => 1976 | println myList.get(0) // => 1976 | ||
| println myList.size() // => 2 | println myList.size() // => 2 | ||
| </code> | </code> | ||
| - | More on lists: [[groovy>JN1015-Collections|usage examples]], [[groovy>groovy-jdk/java/util/List.html|added methods to the standard List interface]] | + | More on lists: [[groovy>JN1015-Collections|usage examples]], [[groovy>groovy-jdk/java/util/List.html|added methods to the standard List interface]], [[ibm>java/library/j-pg04149.html|Reaching for each]] |
| + | |||
| + | ==== Maps == | ||
| + | |||
| + | <code groovy> | ||
| + | myMap = ["Sandy":1976, "Becki":1969] | ||
| + | println myMap.Sandy // => 1976 | ||
| + | println myMap.get("Sandy") // => 1976 | ||
| + | println myMap.class // => null (default getter is overwritten !!!) | ||
| + | println myMap.getClass() // => class java.util.LinkedHashMap | ||
| + | myMap.Junior="2010?" // Add a new value | ||
| + | myMap.each{ print "$it " } // => Sandy=1976 Becki=1969 Junior=2010? | ||
| + | myMap.each{ print "$it.key=$it.value "} // => Sandy=1976 Becki=1969 Junior=2010? | ||
| + | myMap.each{ k, v -> print "$k=$v " } // => Sandy=1976 Becki=1969 Junior=2010? | ||
| + | </code> | ||
| + | |||
| + | More on maps: [[ibm>java/library/j-pg04149.html|Reaching for each]], [[groovy>JN1035-Maps|Maps]] | ||
| ===== Unsorted == | ===== Unsorted == | ||
| Zeile 129: | Zeile 177: | ||
| ===== Open Questions / To do == | ===== Open Questions / To do == | ||
| + | * Grails: http://grails.org/doc/latest/guide/17.%20Deployment.html http://grails.org/Deployment | ||
| + | * [[http://www.ibm.com/developerworks/java/library/j-pg03155/|GroovyServer Pages]] | ||
| * [[http://docs.codehaus.org/display/GROOVY/Extended+Guide+to+Method+Signatures|Named arguments]] are possible. Are they useful? | * [[http://docs.codehaus.org/display/GROOVY/Extended+Guide+to+Method+Signatures|Named arguments]] are possible. Are they useful? | ||
| * Final classes can be [[ibm>java/library/j-pg06239.html|extended]] | * Final classes can be [[ibm>java/library/j-pg06239.html|extended]] | ||