6 May 2004 17:04
Re: isNaN function?
On 6 May 2004, at 14:29, Laforge Guillaume wrote:
> Hey, just for the fun, one more idea: let's use your own groovy
> methods on Strings !
> Now, in groovy, it's possible to define your own groovy methods, your
> own "new" methods on any class you like. So for instance, you can
> extend String with a isNaN() method.
>
> ======================================================================
> class StringUtils {
> static Boolean isNaN(String self) {
> { |s| s.any { !Character.isDigit( (Character)it ) } }.doCall(self)
> }
> }
>
> use(StringUtils) {
> println "123456".isNaN()
> println "123456a".isNaN()
> }
> ======================================================================
>
> You can also define your new methods in a Java file of course.
> But I thought it would be groovier to define this new method in Groovy
>
Neat! :)
James
-------
http://radio.weblogs.com/0112098/
RSS Feed