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.