Posted By

hansamann on 04/17/07


Tagged

boolean groovyseries truth true false


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

jsbournival
bootz15
hendersk
mbanfi
adaptives


Groovy Series: Groovy Control Structures - Groovy Truth


 / Published in: Groovy
 

URL: http://hansamann.podspot.de/files/grails_podcast_episode_40.mp3

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

  1. //boolean expressions are used all over, so we first examine The Groovy Truth
  2. //any type can be evaluated as a boolean expression, a few specialties:
  3.  
  4. //Boolean
  5.  
  6. //Matcher
  7. assert ('Hello World' =~ /World/) //true because matcher has at least one match
  8.  
  9. //Collection
  10. def numbers = [1,2,3]
  11. assert numbers //true, as numbers in not empty
  12. numbers = []
  13. assert !numbers //numbers is now false, at it is an empty collection
  14.  
  15. //The same with maps, empty maps are false
  16. assert ['one':1]
  17. assert ![:]
  18.  
  19. //String
  20. assert 'This is true'
  21. assert !''
  22.  
  23. //GStrings
  24. def s = ''
  25. assert !("$s")
  26. s = 'x'
  27. assert ("$s")
  28.  
  29.  
  30. //Number
  31. assert !0 //yeah, 0s are false, like in Perl
  32. assert 1 //this is also true for all other number types
  33.  
  34. //Any object reference that is non-null is true!

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: hansamann on April 25, 2007
Posted By: hansamann on April 25, 2007

You need to login to post a comment.