Translation of the code block examples from Programming Ruby 2nd Edition to the Groovy Language using Closures
Fri, 9 Feb 2007 08:29 PM GMT
Yesterday, at the Albuquerque Java Users Group, one of our members wanted to see the famous example of code blocks from Programming Ruby 2nd Edition Page 20, done in Groovy.  At the time, we were on the discussion of closures.  I didn't know at the time, what we ended up doing was working on a code block example and not a closure example.   I took a swing at it, and got it working, with Groovy closures.  After the in-meeting hackfest, I came out of the meeting with important simple lessons.

When working in both languages, there are some things to keep in mind, that I happened to have easily forgot:
  • Not all code blocks are closures in Ruby.
  • Using the yield statement in Ruby has nothing to do with closures.
  • {|str, num| ... } in Ruby doesn't necessarily mean {str, num ->... } in Groovy, it entirely depends on what the block is used for and how.
  • Groovy does not have blocks in the sense that Ruby does, so it doesn't have yield.
Given that, here are some variations on the famous code block examples in Programming Ruby 2nd Edition Page 20 by Dave Thomas, done in Groovy..


The Original Ruby Code Block Example:
def call_block
puts "Start of method"
yield
yield
puts "End of method"
end
call_block { puts "In the block" }
The result of this Ruby Code:
Start of method
In the block
In the block
End of method
Groovy Example 1 producing the same results:
def call_block(Closure c) {
println "Start of method"
c.call()
c.call()
println "End of Method"
}
call_block { println "In the Block" }
Groovy Example 2 producing the same results:
def call_block(c) {
println "Start of method"
c()
c()
println "End of Method"
}
call_block { println "In the Block" }
Groovy Example 3 producing the same results:
def call_block = {c->
println "Start of method"
c()
c()
println "End of Method"
}
call_block { println "In the Block" }
Groovy Example 4 producing the same results:
def call_block = {
println "Start of method"
it()
it()
println "End of Method"
}
call_block { println "In the Block" }


The Original Ruby Code Block 2 Example With Parameters:
def call_block
yield("hello", 99)
end
call_block {|str, num| ... }
Groovy Example 1 producing the same results:
def call_block2(Closure c) {
c.call("hello", 99)
}
call_block2 {str, num -> println (str + num)}
Groovy Example 2 producing the same results:
def call_block2(c) {
c("hello", 99)
}
call_block2 {str, num -> println (str + num)}
Groovy Example 3 producing the same results:
def call_block2 =  {
it("hello", 99)
}
call_block2 {str, num -> println (str + num)}
Thanks to John Wilson of The Wilson Partnership for the extra groovy variants, and your help delineating this stuff.
1 comment for Translation of the code block examples from Programming Ruby 2nd Edition to the Groovy Language using Closures
Anonymous
Mon, 12 Feb 2007 01:27 PM GMT

An even more interesting comparison would be between the scopes in each language, and the differences in meaning returns and breaks, within closures.
My 'Groovy in Action' book is still backordered
Wed, 10 Jan 2007 08:30 PM GMT
Last week, I posted my predictions on my blog about what I think is to come in 2007.  At my number 1 spot is the take off of Groovy.  It is an excellent language and a lot of fun to work with.

About four weeks ago, I went to the online Manning bookstore to order the PDF and the book.  I received the PDF online within minutes, but the actual tangible book has not showed up yet.  So, I sent an email to Manning sales and they apologized for the delay. The reason for the delay is that the book is back ordered.  Now, either Manning dropped the ball and forgot to print more, or the demand is too high.  I personally don't think that Manning dropped the ball since I have ordered books from them directly before and it has always been shipped on time or early.

If it is indeed due to popularity of the book, I wouldn't be surprised.  There is going to be a lot of heavy interest in Groovy this year by Java programmers who like to have a ruby-style dynamic language solution without learning a whole new API and have the ability to use their own existing Java code (although I do recommend learning Ruby too). 

So, if you haven't got into Groovy yet and would like to, get in line to buy Dierk Koenig's Groovy In Action.  It is an excellent book and if you go step by step through all it's examples, you will pick up on the language quickly.  If you want to get into Ruby, then there is no better book out there than Dave Thomas' Programming Ruby from Pragmatic Programmer.  Whatever you choose, get into a scripting language if you haven't done so already.  Having a scripting language along side of Java is a powerful combination, and will help you conquer those development iterations with ease. It will be trading in your hammer with a nail gun.
Effective Debugging using Dave Thomas' "Programming Ruby"
Sat, 22 Jul 2006 02:43 AM GMT

Little bastard never saw that closure coming.

5 comments for Effective Debugging using Dave Thomas' "Programming Ruby"
manolai
Tue, 1 Aug 2006 11:57 PM GMT

gonna steal this photo great thanx
Anonymous
Fri, 4 Aug 2006 10:29 PM GMT

I want you to know that I've contacted PETA and IHF2 (Insects Have Feelers Too).
Narayan Iyer
Fri, 6 Oct 2006 07:37 AM GMT

Now, you could have avoided it with the PDF version!!! Now, that's pragmatic!!!
Anonymous
Wed, 11 Oct 2006 02:44 PM GMT

Great!
Dmitry
Sun, 1 Apr 2007 11:12 AM GMT

Funny =)