thejavajar

{ java, groovy, flex, python, ruby }

Flower

Archive for August, 2009

Ruby Proc Fun

I am fairly new to Ruby but I do have experience with Groovy so I have found learning and experimenting with Ruby somewhat of an easy effort. The Ruby community is pretty big so there are plenty of blog posts and other Web sites that are available, via a Google search, to help understand some problems when I get stuck. I started playing around with Ruby Proc (procedures), if/elsif/else and case/switch statements and here is what I am having fun with:

# define our apple Proc
apple = proc { |t| puts "my apple" + t }

Above we have our executable (proc) code assigned to the ‘apple’ variable.

# doIt1 with if statements
def doIt1(c, t)
  if t.respond_to?("call")
	puts "-- callable"
    c.call " is awesome"
	t.call " is really awesome"
  elsif t.kind_of?(Array)
	puts "-- not callable Array"
	t.each { |t1| c.call t1 }
  else
	puts "-- not callable String"
	c.call t
  end
end

Then we have a method ‘doIt1(c,t)’. This method expects 2 arguments; the first argument, ‘c’, I expect is a Proc and the second argument, ‘t’, I expect to be a Proc, Array or String.

if t.respond_to?("call")

The ‘if’ statement checks to see if ‘t’ has a method named ‘call’ that it will respond to. I think the ‘respond_to’ method is great for dynamic languages. I could have just checked to see if ‘t’ is a Proc as well, but then that limits me to one Object type, right?

t.kind_of?(Array)

The ‘elsif’ statement checks to see if ‘t’ is of type Array. Like instanceof in Java.

# doIt2 with case/switch block
def doIt2(c, t)
	case t
	  when Proc
	    puts "-- callable"
		c.call " is awesome"
		t.call " is really awesome"
	  when Array
	    puts "-- not callable Array"
		t.each { |t1| c.call t1 }
	  when String
	    puts "-- not callable String"
		c.call t
	  else
	    puts "not valid"
	end
end

Then I created a method just like ‘doIt1′ called ‘doIt2′. In ‘doIt2′, I used a case/switch statement and handled things similarly to ‘doIt1′. In the second method, I check ‘t’ to be of type String and have a default case for when ‘t’ is not a Proc, Array or String.

# doIt1 tests
doIt1 apple, apple
doIt1 apple, " works good"
doIt1 apple, " works hard"
doIt1 apple, [" looks good", " looks elagant", " looks shiny"]

# *'s used to separate method tests
puts "*" * 40
puts "*" * 40

# doIt2 tests
doIt2 apple, apple
doIt2 apple, " works good"
doIt2 apple, " works hard"
doIt2 apple, [" looks good", " looks elagant", " looks shiny"]

Then I wrote a few ‘tests’ to see what would happen. Ruby is fun and I definitely enjoy working with Ruby and Groovy. Here is the full script file: appleProc.rb

Java on Google App Engine

Google App Engine’s support of Java was a long awaited, highly demanded feature of the service back when I first started messing around with original, Python version a little over a year ago. The excitement that App Engine brought to developers was echoed by the overwhelming request for Java support. Over the past 10 days I have created a few sample applications locally to give it a test. I went the App Engine Eclipse plugin route to get things started. It is a really nice plugin that delivers what you would expect from Google. Once installed, you can create a project and have it running in under a minute.

Once I got familiar with what I was working with, I decided to turn this sample application into a simple Spring Web Flow 2 demo. That is where development stopped. Unfortunately, there are a lot of JVM features/classes that are not accessible on App Engine that Web Flow 2 (and 3rd party libraries) needs for binding and flow execution support. Nothing against Google or SpringSource, I just did not get what I wanted.

Fortunately, I created a new sample app that is based on Groovy’s Groovlet technology and things were moving forward again. Just configure the GroovyServlet in your web.xml file and away you go. My next experiment will be to work with Grails on App Engine via the Grails App Engine plugin.

Overall, App Engine is great. It is really easy to use and the application dashboard features are amazing. Access to application and admin log files in a nice HTML based presentation. Quota detail that explains bandwidth, CPU and datastore utilization along with access to different versions of your application.  You can even grant access to other developers for collaboration efforts. All features you normally do not get from your company’s “Enterprise” admin group.

On a side note, Vladimir Vivien and I will be discussing Google App Engine at the Tampa JUG on August 25th, 2009.

Spring 2.5 Aspect Oriented Programming

Over the weekend I received Spring 2.5 Aspect Oriented Programming. I am pretty excited about reviewing this book because I have been a fan of using AOP with Spring in the past because it can simplify code by extracting things like transaction management and logging into a config file (ie Spring context file), making the application logic more manageable and cleaner.

Getting Familiar with Ruby on Rails

Ruby on Rails is something that I have been meaning to look into for a while. I have been working with Java for most of my professional IT career and I work with Groovy on Grails whenever I have it my way. I even work with PHP and Python when working with some clients or when I feel like doing something different. So, I owed it to myself to give Ruby on Rails a good look. Why? Because there are a lot of people who are passionate about RoR and Rails is what inspired Grails after all.

I have been working with a pretty simple Web application that requires simple security, data access and minimal navigation. I will post more about the application once I have it done, but I can see why people are passionate about RoR just like I am passionate about GoG. Ruby is elegant, no doubt about it and Rails fits in to bring it all together for Web developers who want to focus on solving the problem at hand rather than fighting the technology stack. I am enjoying my experience with RoR thus far and would recommend that other Web developers who have not yet tinkered with Ruby on Rails give it a try for a few hours a night for a couple of days. Check http://rubyonrails.org/ out. There is a plenty of documentation and links to get you started.

You are currently browsing the thejavajar blog archives for August, 2009.