Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

groovy script return value

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kenneth A. Kousen
gunslinger & author
Ranch Hand
Posts: 100
5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That works perfectly. Thanks Ken.
 
Burk Hufnagel
Ranch Hand
Posts: 814
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kenneth A. Kousen
gunslinger & author
Ranch Hand
Posts: 100
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic