Skip to main content.

GroovyMag blog

Blog for Groovy Magazine

Yeah, yeah, OK, I can't name blog post titles very creatively :)

This is simply a repost of a blog post I did a few months ago, but I'm not sure many people here have read my other more generalized blog, so I'll repost the Groovyish content here now and then.

Someone recently asked how to get a random element from an array. In PHP it’s pretty straightforward - array_rand($x) - but there’s not (AFAICT) a built-in way in Groovy to do this. So I wrote a little something which should hopefully demonstrate the power of the metaClass stuff to boot.

ArrayList.metaClass.getRand = { number -> if(number==0) { return delegate[new Random().nextInt(delegate.size)] } else { def tempList = [] def counter = 0 while(counter>number) { tempList.add(delegate[new Random().nextInt(delegate.size)]) counter } return tempList } } def names = ['mike','matt','mark','lesley','jean','ron','jeff','martine'] println names.getRand() println names.getRand(5)

The first call to getRand() on the names list will just return one name from the list. The second will return 5 random entries (and some could theoretically be repeated). I’ve added this getRand() method to the base ArrayList class at runtime, even though it’s likely declared ‘final’ in Java’s base libraries. This is one of the core powers of Groovy, and is pretty slick, imo.

I just learned about a new upcoming book from Apress about Groovy and Grails - Groovy and Grails Recipes, by Bashar Abdul Jawad You can preorder the book from that link. From what I gather, it's supposed to get released December 14th, just a couple weeks from now! This would make a great holiday gift for that Groovy developer in your life! I only wish someone from Apress would have let us know about this sooner - would loved to have done some sort of promotional tie in with GroovyMag!

The December issue of GroovyMag is out - go grab it now!

This issue has some great articles in it, including Christopher Judd's article on iPhone development with Grails and the iUI plugin, Andres Almiray's continuing series on Swing applications in Groovy, and more. Just read the above link or watch the video there to get an idea of what you'll be getting.

Andres, Christopher and myself will all be presenting at codemash in January, so if you're interested in meeting some of the GroovyMag writers, book your ticket now.

We've got some great content lined up for the next few issues, including a look at Grails webflows, Groovy regular expressions, and a sneak preview of the upcoming "Grails in Action" book (Manning) from Glen Smith and Peter Ledbrook. If you're not in to Groovy and Grails yet, now's a perfect time to jump on board. It's getting better all the time!

Just a quick 'hello' to GroovyMag readers and followers to wish them a Happy Thanksgiving day today! For those of you not based in the US, I all hope you enjoy your day all the same! Sympathies go out to those in Mumbai affected by the attacks yesterday, and that news makes me all the more appreciative of the family and friends I have around me these days.

Thanks everyone for a great November so far. Couldn't have expected this amount of support for a first month's launch, and it's truly appreciated.

I wanted to give y'all a quick update on how we're progressing, because I've had a number of questions from many of you on these issues.

Print copies

We're still investigating print copies of the magazine. Some of you have indicated that you're not interested in print, but many of you have expressed a desire for print. As you can probably guess, print costs a lot extra to produce. Between the printing and the shipping costs, that might add an extra $4 or $5 on to the cost, and we need to commit to a certain amount of printed copies every month to get any sort of volume pricing. Print copies are something we'll continue to look in to, but probably won't be a reality until GroovyMag is a few more months older (first quarter of next year, perhaps). But we are still looking for ways to make this happen.

Subscriptions

We're looking at ways to organize and manage a yearly subscription to GroovyMag. This may be in place for the January edition, so keep your eyes peeled for this.

Licensing

We've had multiple inquiries from companies interested in licensing GroovyMag for their engineering staff. This is being worked on as well, and I also expect this to be in place for Q1 of next year, possibly for the January issue. If you're interested in this, please email editor@groovymag.com to discuss the details. Site licenses will be priced per reader, with group discounts available.

Thanks!

Again, thank you everyone for making the first month of GroovyMag, well... Groovy, and we're looking forward to bringing you more great Groovy and Grails content to help improve your skills.