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
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:
The Original Ruby Code Block Example:
The Original Ruby Code Block 2 Example With Parameters:
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
yieldstatement 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.
The Original Ruby Code Block Example:
def call_blockThe result of this Ruby Code:
puts "Start of method"
yield
yield
puts "End of method"
end
call_block { puts "In the block" }
Start of methodGroovy Example 1 producing the same results:
In the block
In the block
End of method
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_blockGroovy Example 1 producing the same results:
yield("hello", 99)
end
call_block {|str, num| ... }
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.
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.