File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Groovy and the fly likes groovy script return value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "groovy script return value" Watch "groovy script return value" New topic
Author

groovy script return value

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31062
    
232

When I run the following script in the Hudson/Jenkins groovy console, it outputs something like:
1
2
[1,2]



I understand that the first two lines are coming from the println and the last line is the result. How do I tell Groovy that I don't want the result returned/printed?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Kenneth A. Kousen
gunslinger & author
Ranch Hand

Joined: Sep 18, 2002
Posts: 89
    
    5
The Groovy console returns the last evaluated expression, which in this case is the list. The easiest way to prevent that is to add something else. So, for example, just add the word "null" or "false" on the line after your expression, and that's what will be returned.

As an aside, if you run your script in Eclipse, you won't see the last value. Or, more properly, the script will return the last value but Eclipse doesn't print it.


Kenneth A. Kousen, Ph.D. (assorted certs), President, Kousen IT, Inc.
Author of Making Java Groovy - http://www.kousenit.com
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31062
    
232

That works perfectly. Thanks Ken.
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Kenneth A. Kousen wrote:The Groovy console returns the last evaluated expression, which in this case is the list. The easiest way to prevent that is to add something else. So, for example, just add the word "null" or "false" on the line after your expression, and that's what will be returned.

As an aside, if you run your script in Eclipse, you won't see the last value. Or, more properly, the script will return the last value but Eclipse doesn't print it.


So does Groovy always returns a value? Or, is it something the Groovy Console does - which could explain the difference in behavior you're seeing in Eclipse.

Thanks,
Burk


SCJP, SCJD, SCEA 5 "Any sufficiently analyzed magic is indistinguishable from science!" Agatha Heterodyne (Girl Genius)
Kenneth A. Kousen
gunslinger & author
Ranch Hand

Joined: Sep 18, 2002
Posts: 89
    
    5
The value of a Groovy scripts is the value of their last evaluated expressions. In Eclipse, the IDE chooses not to print the value of the script to the console. The Groovy console does.

The last evaluated expression in both methods and closures are also automatically returned.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31062
    
232

Ken,
That explains why I hadn't seen this before. I mostly used Eclipse. And even in the groovy console, I started out experimenting with for loops which don't return anything. I'm finally comfortable enough to use closures in loops which is how this came up.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: groovy script return value