This snippet gives you some interesting examples about Groovy Truth. Listen to the audio to get the best learning experience. Simply right-click on the title above to download the mp3 file of this part of the series.
The Groovy Series is part of the Grails Podcast and can be subscribed to via: http://hansamann.podspot.de/rss. The series is produced by Dierk König and Sven Haiges, further information about the topic of this episode can be found in the book
Groovy in Action - http://groovy.canoo.com/gina
- //boolean expressions are used all over, so we first examine The Groovy Truth
- //any type can be evaluated as a boolean expression, a few specialties:
- assert ('Hello World' =~ /World/) //true because matcher has at least one match
- def numbers = [1,2,3]
- assert numbers //true, as numbers in not empty
- numbers = 
- assert !numbers //numbers is now false, at it is an empty collection
- //The same with maps, empty maps are false
- assert ['one':1]
- assert ![:]
- assert 'This is true'
- assert !''
- def s = ''
- assert !("$s")
- s = 'x'
- assert ("$s")
- assert !0 //yeah, 0s are false, like in Perl
- assert 1 //this is also true for all other number types
- //Any object reference that is non-null is true!
CommentsSubscribe to comments
You need to login to post a comment.