Pears of Wisdom - Groovy Fundamentals

Alex Poiry
Note by Alex Poiry, updated more than 1 year ago
Alex Poiry
Created by Alex Poiry about 3 years ago


The basics of Groovy

Resource summary

Page 1

High Level Groovy Concepts TODO   Basic Syntax System.out.println("Hello Groovy") ;  is the same as, println("Hello Groovy");   is the same as, println("Hello Groovy")   is the same as, println "Hello Groovy" Groovy will let you write Java-like syntax, but really only needs basic ruby-like syntax.   def  :  used to define variables   String Interpolation is done like this: def greeting = "Hello, " println "$greeting" + "Groovy"   Adding the { } characters tell string interpolation to apply to a whole expression or variable, like this: def greeting = "Hello, " def who =  ["Groovy", "World"] println "$greeting" + "${who[0]}"   Idiomatic Groovy uses double quotes for string interpolation only, otherwise use a single quote.   Looping def greeting = "Hello, " def who =  ["Groovy", "World"] for (name in who) {   println "$greeting" + "$name" }   Ranges def numbers = 1..10   :    This creates a collection, it also works with things like letters and enums println numbers.from    :    This equals 1 println     :     This equals 10   Functions def camelCaseName(val) {   "Sweet" } Note that the last line in the method is returned by default. You can explicitly call return if you need multiple return points.   Closures def myClosure = { println "A closure" } The each method can accept a closure Each has an implicit reference to the current item in the iteration, which is 'it', like this: def who =  ["Groovy", "World"] who.each({ println it }) This will print Groovy, then World If you have multiple iterators you can explicitly define the current iteration like this: who.each({ name -> println name })   Dump & Inspect You can print out a lot of information about an object by calling dump.  This should provide you the object type, its inheritance hierarchy, as well as its members There is also a method called inspect.  Developers are expected to override inspect with useful information.  If they haven't don that, it defaults to the toString() method for the object.   With This is a helper method on objects called with.  It accepts a closure and prevents you from having to continually re-type a variable name whose dot methods you are accessing, like this: data.with {   println item   println thing } AS OPPOSED TO println data.item println data.thing   Groovy Truthfulness Any not null value evaluates to true on a boolean comparison, like this: if (!value) AS OPPOSED TO if (value == null)   Categories TODO

Page 2

Some Basic Activities   def file = new File('../path/to/file.xml') def slurper = new XmlSlurper() def data = slurper.parse(file) println data.elementName println data.@attribute println data.elementA.elementB println data.elementA.elementB.@otherAttr   XmlSlurper will also dynamically determine the type of XML object and try to convert it, so you can run collection methods on XML arrays, etc.

Show full summary Hide full summary


Language Development
Development and Inequality Junior Cert
Gaeilge SPC
Useful String Methods
Shannon Anderson-Rush
Historical Development of Computer Languages
Shannon Anderson-Rush
Polymer 2.0 - Custom Element - Register Element
Ravi Upadhyay
Code Challenge Flow Chart
Charlotte Hilton
Flvs foundations of programming dba 2
mariaha vassar
GCSE Biology Module B5
HPS202 exam revision
Piaget's Theory of Cognitive Development
Anja Starc