Groovy: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 5: Line 5:
<Font color=red>TODO Groovy basics: https://docs.gradle.org/current/userguide/writing_build_scripts.html#groovy-dsl-basics</font>
<Font color=red>TODO Groovy basics: https://docs.gradle.org/current/userguide/writing_build_scripts.html#groovy-dsl-basics</font>


=String Programming=
=<span id='String_Programming'></span>Strings=


<font color=red>TODO next time I need it.</font>
<font color=red>TODO next time I need it.</font>
Line 16: Line 16:
def x = 10
def x = 10
println "result is $x" // prints: result is 10
println "result is $x" // prints: result is 10
</syntaxhighlight>
==Rules for String Interpolation==
Groovy supports declaring a string with either single quotes or double quotes.
If the string is single-quoted, the variable interpolation behavior is turned off, similarly to how [[Bash_Parameter_and_Variable_Expansion#Overview|bash handles single-quoted strings]].
<syntaxhighlight lang='groovy'>
def user="Elemental"
echo 'Hello ${user}'
</syntaxhighlight>
will output:
<syntaxhighlight lang='text'>
Hello ${user}
</syntaxhighlight>
Double-quoted string support dollar-sign based interpolation:
<syntaxhighlight lang='groovy'>
def user="Elemental"
echo "Hello ${user}"
</syntaxhighlight>
will output:
<syntaxhighlight lang='text'>
Hello Elemental
</syntaxhighlight>
</syntaxhighlight>



Revision as of 22:15, 17 April 2020

TODO

  • Tutorial: www.tutorialspoint.com/groovy/index.htm

TODO Groovy basics: https://docs.gradle.org/current/userguide/writing_build_scripts.html#groovy-dsl-basics

Strings

TODO next time I need it.

Single-Quoted vs. Double-Quoted Strings

Groovy has both double-quoted and single-quoted String literals. The main difference is that double-quoted String literals support String interpolation:

def x = 10
println "result is $x" // prints: result is 10

Rules for String Interpolation

Groovy supports declaring a string with either single quotes or double quotes.

If the string is single-quoted, the variable interpolation behavior is turned off, similarly to how bash handles single-quoted strings.

def user="Elemental"
echo 'Hello ${user}'

will output:

Hello ${user}

Double-quoted string support dollar-sign based interpolation:

def user="Elemental"
echo "Hello ${user}"

will output:

Hello Elemental

Working with Closures

http://groovy-lang.org/closures.html

Defining a Closure

def myClosure = { e -> println "Clicked on $e.source" }

Implicit Paramenter

When a closure does not explicitly define a parameter using the '->' syntax, the closure 'always defines an implicit parameter named "it".

Passing Closures to Methods

If the closure is the last argument for a method, it can be passed outside the argument list.

https://mrhaki.blogspot.com/2009/11/groovy-goodness-passing-closures-to.html

Template Engines

Groovy Template Engines

Files

filename = 'example.txt'
File f = new File(filename)
def lines = f.readLines()
for (line in lines) {
    // ...
}
filename = 'example.txt'
File f = new File(filename)
f.eachLine({
 if (!it.startsWith("#") && !it.trim().isEmpty()) {
   ...
  }
})

Data Structures

Map

def artifacts = [

        "chart-A": [
                "watchFor": ["src/charts/chart-A"],
                "chartName": "a",
        ],
        "chart-B": [
                "watchFor": ["src/charts/chart-B"],
                "chartName": "b",
        ],
        "script": [
                "watchFor": ["src/bin/run", "src/ansible", "script/bin/lib/a.shlib"],
        ]
]