By Developers, For Developers

Historical errata for Programming Groovy 2

PDF PgPaper PgTypeDescriptionFixed onComments
3TYPO

“As we see here, it’s real easy…”
should be
“As we see here, it’s really easy…”

2013-02-24
11TYPO

“Closure’s in Groovy are quite exceptional”
should be
“Closures in Groovy are quite exceptional”

2013-02-24
28TYPO

“Let’s wet our appetite by making use…”
should be
“Let’s whet our appetite by making use…”

2013-02-24
89TYPO

“Closure in Groovy are short anonymous methods…”
should be
“Closures in Groovy are short anonymous methods…”

2013-02-24
0TYPO

Reading ePub version, therefore no page numbers.

Introduction => Acknowledgements

Section Title “Acknowledgments” should be Acknowledgements

Then further down in in 4th para:

“time to write the forward” should be “foreword”.

2013-06-16
20SUGGEST

A minor point: On p. 20, you compare a Java class containing main() with a Groovy script. I understand your point that in Groovy you can just call a class in a script without using a main() method. However, the two versions are not directly equivalent, precisely because the Groovy script has lost the ability to be the main() function in a program with other modules, whereas the Java program has not. If you want to say the Groovy version is a shorter version of the Java version, I think you should point out that the Groovy version has slightly less capacity. Or perhaps show a second version of the Groovy example using main(). You do the same type of comparison with Java main() classes elsewhere in the book. If you search for “main” you’ll find other examples.

400SUGGEST

No example of main() in Groovy anywhere in the book? Seems like an important omission.

xviiTYPO

soooo

xviiiTYPO

and memory today than large computers had decades ago.

it should be

and memory today is larger than the large computers had decades ago.

5ERROR

groovy:000> ‘mom’.l
ERROR groovy.lang.MissingPropertyException:
No such property: l for class: java.lang.String
at groovysh_evaluate.run (groovysh_evaluate:2)

I am using JDK 1.6 and groovy 2.1.6.

16ERROR

println “groovy -v”.execute().text

This didn’t work for me on Windows 7. I get the following error.

Caught: java.io.IOException: Cannot run program “groovy”: CreateProcess error=2, The system cannot find the file specified
java.io.IOException: Cannot run program “groovy”: CreateProcess error=2, The system cannot find the file specified
\tat com.example.Greetings.run(Greetings.groovy:23)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
\t… 1 more

xviiERROR

You list a method for computing a 5% raise on 80,000. That would be 4,000. However, your method is this:

Integer.metaClass.percentRaise = { amount -> amount * (1 + delegate / 100.0) }

The “1 +” part here does not make sense to me. I would think the calculation would be:

80000 * (5 / 100.0)

Which would mean the method should be this:

Integer.metaClass.percentRaise = { amount -> amount * (delegate / 100.0) }

A small issue, perhaps, but it might also be highlighting something I’m not understanding.

16ERROR

You have an example like this:

println “groovy -v”.execute().text

You say: “This code sample works on Unix-like systems and on Windows.”

However, when I test this out on a Windows machine, I find that it gives the following error:

Caught: java.io.IOException: Cannot run program “groovy”: CreateProcess error=2, The system cannot find the file specified

Note that “groovy” does work on my command line; I have GROOVY_HOME set and the GROOVY_HOME\\bin on my path. I can run groovy and groovysh from the command line without issue.

You have to do this:

println “cmd /C groovy -v”.execute().text

You do mention the need to run ‘cmd’ directly later on the same page. You mention that you need to do this if you use the “dir” command but that’s not true. You can run this:

println “dir”.execute().text

without error.

47SUGGEST

I would consider that some of your readers would experiment and maybe anticipate confusions. For example, on the WhatEquals script, you only run two tests:

new A() new A() new B() new B()

What I would do — and, in fact, did — was this:

new A() new A() // equals new A() new B() // equals
new B() new A() // ? new B() new B() // compareTo

That’s relevant because notice the third one? Groovy returns nothing at all. Maybe that makes sense to someone. But I’m certainly not sure why that result is the way it is; and this seems like a perfect spot in the book to explain why that is.

22SUGGEST

On this page, you say:

“To take advantage of this feature, define the first parameter as a Map.”

However, it’s not clear to me that any of the parameters in the Robot class are a Map. A bit further down you say:

“The instance of Robot took type, height, and width parameters as name-value pairs.”

Okay, so it “took” them that way — but I didn’t define anything as a Map, did I? This is the part I’m not sure about. The confusion here is probably mine but I wonder if the wording could be clarified?

26SUGGEST

I recommend an attempt to anticipate how people experiment. For example, on this page, I can try examples like this:

def (verbPhrase, directObject, preposition, indirectObject) = [‘ask’, ‘bob’, ‘about’]

That works in that even though I have four variables only three are returned. However, this does not work:

def parse(command) { command.split(’ ’) }

def (verbPhrase, directObject, preposition, indirectObject) = parse(“ask bob about”)

It throws an exception. This is the case even though my parse() method is returning an array, which would seem to make my second example the same as the first.

While I realize my examples are different from yours, they are logically the same and the likely experimentation path of most programmers reading this page. I bring this up because on this page, you say: “If the excess variable is a primitive type, something that can’t be set to null,
then Groovy will throw an exception.”

But, again, clarifying with example is better than just words, particularly since my experimentation leads to a question as to how things are working.

This is even moreso the case when you consider that this slight variation does work:

def (verb, noun) = parse(“ask bob about the alien invasion”)
println “Action ‘$verb’ is applied to the object ‘$noun’”

16ERROR

Instead of the following which doesn’t work on windows.
println “groovy -v”.execute().text

The following works on windows 7. since it’s a shell command.

println " cmd /c groovy -v".execute().text

Groovy Version: 2.1.6 JVM: 1.6.0_35 Vendor: Sun Microsystems Inc. OS: Windows 7

281ERROR

At this point in the book, when I run the test you have described, it does not pass as this page says it should. Rather I get this:

testMyMethod(TestCodeWithHeavyDependenciesUsingOverriding)junit.framework.AssertionFailedError: expected:<35> but was:

It looks to me like the someAction method in CodeWithHeavierDependenciesExt is not actually being executed.

If I put output lines in both someAction() methods, I’m finding that only the one in the original class, not the Ext class, is executing.

281TYPO

(I really wish these errata sections had an edit aspect so you can edit your own previous entries.)

For the last one I posted on page 281 — (entry by Jeff Nyman) — it appears I was running the Java code version and that’s why I was seeing the error I was seeing. Part of the challenge is I was also going through the code from the downloadable distribution which has a ton of files in it. So the problem I report here is actually for the test that appears on page 282 and 283.

112SUGGEST

You mention an eachWithIndex method but don’t show its usage. Granted, someone can look it up but it wouldn’t necessarily hurt to throw one line of code to showcase this:

lst.eachWithIndex { obj, i -> print “${i}: ${obj} ” }

Or something like that anyway.

113SUGGEST

You mention the difference between iterating with each() and collect() but it’s mostly words and two disjointed examples. I think it might be better to show the difference like this:

my_list = [1, 3, 4, 1, 8, 9, 2, 6]
doubled = []

result = my_list.each { it * 2 }
result_d = my_list.each { doubled << it * 2 }
println result
println result_d

result = my_list.collect { it * 2 }
println result

I think examples like this can make the point a lot clearer.

74SUGGEST

This page has the following text:

“It stretches its hands and reaches out to the
variable product in the scope of the caller of pickEven().”

In my opinion, wording like that tends to hide rather than clarify meaning. I would more just state what is happening, particularly since the above can be misleading.

50TYPO

First paragraph after code block at the top of the page

class name referenced has a typo - Calibrartor instead of Calibrator.

85TYPO

Minor nit - completeOrder() instead of computeOrder() in the first paragraph

141ERROR

Chapter 7 (Exploring the GDK) under section 7.3: Custom Methods Using the Extension Modules. In Extension/FindPrice.groovy, the static and instance methods are switched up.

You book says the following:
def ticker = “ORCL”

println “Price for $ticker using instance method is ${String.getPrice(ticker)}”
println “Price for $ticker using static method is ${ticker.getPrice()}”

It should be something like this:
def ticker = “ORCL”

println “Price for $ticker using static method is ${String.getPrice(ticker)}” // Changed instance to static
println “Price for $ticker using instance method is ${ticker.getPrice()}” // Changed static to instance

141TYPO

Possible swapping of instance vs. static methods - just the labeling with the println of which method is static and which is instance. First one is labeled as instance but is static and vice-versa on the second println.

70TYPO

Chapter 3: Static Compilation
“javac -p NoStaticCompile”

Should be
“javap -c NoStaticCompile”

(ie. the ‘p’ and ‘c’ are swapped incorrectly)

253TYPO

Last paragraph sentence:
Let’s look at an example of creating XML documents in Groov using a builder:
should have Groovy instead of Groov

1741SUGGEST

Note that the page number (1741) entered is the Kindle edition’s Location.

In the section about ‘==’ in groovy versus java (WhatEquals.groovy is the source code) it would be good to mention that the operand on the left decides whether compareTo or equals is called.

So, in your example, new A() new B() would output 'equals called' whereas new B() new A() would output ‘compareTo called’

Also note that if the objects are in fact the same reference, neither equals nor compareTo are called:

def a = new A()
a == a // no output from this

135ERROR

The MODRANGEOUTPUT of “WorkingWithCollections/CreatingArrayList.groovy” produces a different output other than described in the book.

Expected (see PDF page 135):
—————-
//START:MODRANGEOUTPUT
<java.util.ArrayList$SubList@fedbf parent=[1, 3, 4, 1, 8, 9, 2, 6] parentOffset=2 offset=2 size=4 this$0=[1, 3, 4, 1, 8, 9, 2, 6] modCount=1>
After subLst[0]=55 lst = [1, 3, 55, 1, 8, 9, 2, 6]
//END:MODRANGEOUTPUT

Actual result (Using Groovy 2.2.1 JVM: 1.7.0_13 on Mac OS X)
———————————-
//START:MODRANGEOUTPUT
<java.util.ArrayList@fedbf elementData=[4, 1, 8, 9] size=4 modCount=1>
After subLst[0]=55 lst = [1, 3, 4, 1, 8, 9, 2, 6]
//END:MODRANGEOUTPUT

35SUGGEST

In script UsingCoffeeSize.groovy, the for-each loop can be
for(size in CoffeeSize) rather than for(size in CoffeSize.values())

37TYPO

MethodMissingException should be MissingMethodException

xviiTYPO

Soooo… is not a typo but the author is stressing “So”.

52TYPO

def arr = [1, 2, 3, 4, 5] as int[]
println arr2

variable ‘arr’ should be ‘arr2’.

152TYPO

At end of section 9.1, command to create the database without password should be:
mysql -u root < createdb.sql

..or with password:
mysql -u root -p < createdb.sql

It does not work with the equals sign.

Categories: