Skip to content

Instantly share code, notes, and snippets.

@dexterous
Created March 3, 2011 15:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dexterous/852887 to your computer and use it in GitHub Desktop.
Save dexterous/852887 to your computer and use it in GitHub Desktop.
No more if's; SmallTalk style conditionals in Groovy
class BooleanCategory {
public static Boolean then(Boolean self, Closure c) {
if(self) c()
return self
}
public static Boolean otherwise(Boolean self, Closure c) {
if(!self) c()
return self
}
}
use(BooleanCategory) {
('foo'.length() > 2).then { println 'yipee' }.otherwise { println 'bummer' }
}
@dexterous
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment