HomeBlogGet Quote ABQJUGLogin

Syndication

RSSrssAtomatom

Translation of the code block examples from Programming Ruby 2nd Edition to the Groovy Language using Closures

Written by: Dan HinojosaFri, 9 Feb 2007 12:29 PM PST .
Filed Under: GroovyDave ThomasRubyClosures
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 05:27 AM PST

An even more interesting comparison would be between the scopes in each language, and the differences in meaning returns and breaks, within closures.

Albuquerque Java User Group: The Groovy Month

Written by: Dan HinojosaSun, 4 Feb 2007 10:52 AM PST .
Filed Under: Groovyjavausergroup
This is a meeting announcement for the Groovy Month.  Yeah! Baby!

Since we did not have a meeting in January, we will be having a meeting on both February 8th, and February 22nd.  What is the occasion?  On these two days we will be covering the new 1.0 release of the Groovy Programming Language.   There is quite a bit to this new programming language so it requires two days of coder fun!  Our meeting for both days will be from 3:00 PM to 5:30 PM at the Cherry Hills Library which is located at 6901 Barstow NE.  For more information about the Library including a map please go to http://www.cabq.gov/library/branches.html#2.

So, what is the Groovy programming language, and what does it have to do with the Java Programming Language?  Well the Groovy Programming is a scripting language that has been modeled after Ruby, Perl, Smalltalk, Python and of course Java.  The Groovy Language's API is java centric.  What that means is that we now have our very own dynamic scripting language that gets converted to Java byte code and runs against Java APIs.  The Groovy Language also has new features and concepts that is not available to the current java programming language like dynamic lists and maps, builders, closures, and GStrings (hee hee).  Don't worry you won't see me in a GString.

For a taste of Groovy, our "Hello World" example would like this in Groovy:

println "Hello World"

WOW!  No public static void main(String[] args) there!

How about this one, this goes to the Google website, and prints out the HTML:

new URL("http://www.google.com").eachLine {line -> println line}

WOW!  All in one line, with no try, catch, finally statements! 

So this meeting will be divided in two.  In our first meeting, we will start with the basics of the language, including closures. Our second meeting will cover some of the advanced things like using Groovy for testing java applications and some advanced builder techniques. Hopefully we will also have time to look at Grails, which is the Groovy web application framework alternative that is modeled after Ruby on Rails, and will make Ruby on Rails pee on the carpet like a newborn puppy. ;)

Most of the meetings I do are member controlled, which means it is driven by your questions and experiments, which makes it a great way to learn. So come on down!

The ABQJUG is always free of charge and everyone interested in Java is welcome to attend. If you wish to get on the email list or know of someone that would like to be on the list, please write me at dhinojosa@evolutionnext.com.

Meeting information is also on our website at http://www.abqjug.org

Thank you, and we will see you there.

Danno

My 'Groovy in Action' book is still backordered

Written by: Dan HinojosaWed, 10 Jan 2007 12:30 PM PST .
Filed Under: GroovyDave Thomasdierkkoenigrubyscriptingmanning
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.

Implementing Interfaces In Groovy.

Written by: Dan HinojosaMon, 1 Jan 2007 02:53 PM PST .
Filed Under: Groovyinterfaces
Learning how to implement interfaces in the Groovy programming language has been information that has eluded me since I started learning Groovy 3 weeks ago.  Last week, Greg Barr and Paul King has written up a quick and easy tutorial about how to implement interfaces in Groovy.

Here is an example of implementing an iterator, just to show you how clean and sexy it is:

iter = [ i: 10,
         hasNext: { impl.i > 0 },
         next: { impl.i-- }, ] as Iterator

while ( iter.hasNext() )
       println iter.next()

More information and strategies are available at the link, just to save you from all that typing.  The information was also copied to the Advanced OO section of the User Guide.

Woot! Dan Hinojosa's (that's me) java predictions for 2007

Written by: Dan HinojosaFri, 29 Dec 2006 02:10 PM PST .
Filed Under: SeamGroovyRuby2007EJB3JavaGrailsRailsJBoss
Predictions for 2007:

Just a minute while I take out my crystal ball. It is currently sitting in my garage, caked over with dust. There! All dusted off and just like new.
Here are my predictions for the year 2007. Take note, because all of these will come true. Eat your heart out Nostradamus.

  • Groovy will enjoy great success. People will love the idea of having a powerful scripting language that uses the well known Java API at their disposal. Groovy will bring about the repatriation of several Ruby users that defected from Java.
  • Grails will see some success, but that success won't come 'til late 2007. This is because people need time to learn Groovy first, and the Grails developers will probably be working diligently towards version 1.0 throughout 2007.
  • Developers will realize that SOA has nothing to do with Web Services. The SOA community will also gain a general understanding of what SOA really is, hopefully before SOA 2.0 comes out. ;)
  • The Java community will continue to seek clarity on the future of Java and whether or not Java is meant to be an easy language to learn. Such ruminations will lead to more great debates on generics, in-line XML, the arrow operator, and whether it is Java's manifest destiny to include features from other languages in the JDK in order for Java to remain on top.
  • EJB3 will finally be released by major vendors and will gain acceptance by developers.
  • JBoss Seam will gain acceptance by many web developers, but will continue to have problems selling its idea because those same web developers will have difficulty wrapping their heads around what stateful development is and how it's useful to them.
  • Legions of well-known web and desktop Java developers will be bored with web and desktop development and will start to get creative with Java on other devices.
  • JUnit dominance will be relinquished to TestNG.
  • Ant will be demoted by the masses as merely a vehicle for scripting to get builds done.
  • Dependency Injection and Interface Oriented Design will continue to be accepted by Java Developers.
  • The Google Web Toolkit will be the most talked about Java based product in 2007. It will bring non-Java developers to the Java language, eager to do outstanding things with AJAX without the need to code in JavaScript.
  • With systems like Subversion/CVS, Google Calendar, Google Docs, Feature/Bug Tracking software, Gmail, Blogs and Wikis. Developer machines will be and should be relegated to being hard drives dedicated to holding only the JDK, version controlled source code and libraries, and music files that developers listen to while coding. 

Now, go in peace, and spread the good words of what I have foreseen and given to you. ;)
Happy New Year!
Danno!