Friday, November 30, 2007

Interesting Debate...

Martin Fowler brings up a good topic on his blog : "GroovyOrJRuby"

I like the way Martin ends this post... Ruby (and Rails) have brought the dynamic (we used to call them scripting) languages back to the forefront.

There is a need for different languages, to help the programming/software engineering world grow. Finding different ways to do things. The same way we have Ruby, Python, Perl, PHP... we should have JRuby, Jython, PHP/Quercus, Groovy.

Which language is the best for the JVM? I would not jump right into saying Java... it really depends on what you are doing.

Sunday, November 25, 2007

Simple file IO in different dynamic languages

Was just messing around and thought I would look at the different ways to skin a cat... in this case, the cat being "read each line of a file and spit out it's contents". I'm using TCL, Python, PHP, Ruby, Groovy, Scala and Javascript (Rhino) for this example. Let's take a look at what I came up with:



[sourcecode language="css"]
set fp [open "test.txt" r]

while {[gets $fp line] != -1} {
puts stdout $line

close $fp[/sourcecode]
Here we need to open the file pointer and loop over the file using a procedure call to 'gets' which sets a local variable 'line' for us. ('gets' is wrapped in brackets so we can validate on it's return, the length of the string returned by gets proc call.) Finally we will need to close our file pointer.


[sourcecode language="python"]
f = open("test.txt")
for line in f:
print line.strip()
Similar to TCL, we are getting our file pointer (in this case an object), looping the file object and closing the pointer. Version 2.6 will support the 'with' keyword allowing you to wrap the file IO in a block (therefor closing the file pointer for you after executing the block.) Here's what it will look like:
[sourcecode language="python"]
from __future__ import with_statement # <-- only use this line for Python 2.5!

with open("test.txt") as f:
for line in f:
print line.strip()[/sourcecode]


[sourcecode language="php"]
foreach (file('test.txt') as $line) {
echo $line;
This code looks 'blockish', but we're actually calling a function 'file' which returns the file's contents as an array... the file pointer is created and closed within this function so we don't have to worry about closing it in this example.


[sourcecode language="ruby"]
#'test.txt', 'r').each do |line| <-- BAD: open file pointer
File.foreach('test.txt', 'r') do |line| <-- CORRECTION
puts line
UPDATE: was leaving file pointer open... thanks for the correction Sebastian Hungerecker.

Now we are getting OO with blocks. We're creating a File object and calling that object's 'each' method which iterates over each line of the file and passes the contents to the supplied block.


[sourcecode language="java"]
new File("test.txt").eachLine { line ->
println line
Again, OO with blocks... very similar to Ruby.


[sourcecode language="css"]

for {
(line) <- Source.fromFile("test.txt").getLines
} print(line)[/sourcecode]
Procedural type programming using a language that 'goes both ways.' Similar to the PHP code above, the file IO is wrapped up in the 'Source.fromFile' method call which returns a 'Source' object already populated with the file's contents. We then call the getLines method which returns an iterable collection.

Javascript (Mozilla Rhino)

[sourcecode language="javascript"]
lines = readFile("test.txt").split("\n");
lines.pop(); // <-- last item is empty... EOF?

for (i in lines) {
Using a Rhino built-in 'readFile' to get the contents of the file, splitting on line breaks using OO method call, and finally looping the array and printing it's contents. You see something I had to do here to get rid of the empty item in 'lines'.


Update: I failed to mention, these times include JVM startup so it's not a true speed check for IO... rather from the command line experience (total execution time of one call.) This can paint a bad light on the JVM impls, but keep in mind the JVM is built for long running rather than one shot processes.

Of course I was curious... what kind of speed do these languages have for this basic IO? The test file contained only the following:


I don't have the latest and greatest for a few of these... but here are the versions and real time taken. (using 'time' on Mac OS X 10.4.10)

Native Impls

[sourcecode language="css"]
RUBY: 0m0.009s (1.8.6)
TCL: 0m0.012s (8.5)
PHP: 0m0.024s (4.4.7)
Python: 0m0.034s (2.5.1)[/sourcecode]

JVM Impls

[sourcecode language="css"]
RHINO: 0m0.310s (1.6R2)
GROOVY: 0m0.885s (1.0)
SCALA: 0m1.312s (2.6.0)
JRUBY: 0m1.668s (1.1b1)[/sourcecode]What does all of this mean? Not much really... just that there are a lot of different ways (many of which I didn't cover here) to skin a cat. Just choose the one that fits your needs and have fun with it.

Did I miss your favorite language here? Did I not use the most optimal code for accomplishing this task? Let me know!

Friday, November 23, 2007

Swing'in with Ruby and Javascript

I did a presentation last fall about Mozilla's Rhino (a Javascript engine found here.) One of the things demonstrated was the use of Java's Swing from Javascript. It's a tiny bit of simple code so I thought it might be interesting to look at the similarities and diffs between Rhino and JRuby's Java integration from this perspective.

Javascript (Rhino)

[sourcecode language="js"]

frame = new JFrame("My New Window")

panel = new JPanel()

button = new JButton("Click Me")

function clicked() {
print("button was clicked")




Ruby (JRuby)

[sourcecode language="ruby"]
require "java"

include_class %w{

frame ="My New Window")

panel =

button ="Click Me")

listener = ActionListener.impl do
puts "button was clicked"




click me


  1. short and sweet, much nicer than writing this tiny thing in Java

  2. only one JVM specific call in both : importPackage and include_class respectively


  1. Java support automatically 'on' in Rhino : no need to call something to initiate the Java support (JRuby: require 'java')

  2. Rhino has fewer lines : yes, part of this is how I include the classes, but not all...

  3. specifying the ActionListener : Rhino simply allows a function to be passed in... JRuby needs to have an impl of the ActionListener interface... which requires me to include that class

  4. speed : Rhino is faster for this silly test... this is most definitely due to the sheer size of the JRuby impl. Rhino comes in just about 3 times faster than JRuby.


Either way, it's much easier to work with than writing the same thing in Java. JRuby now has a few libraries to help you build your JRuby Swing apps:

I have not seen anything for Rhino, but that doesn't mean they don't exist!

ActiveRecord Migrations - JRuby

I was goofing around a while back, wanted to use AR in a Java project... came up with this Rakefile. Comments welcome, didn't spend much time on it so I'm sure there may be some issues:

[sourcecode language="ruby"]
require 'active_record'
require 'active_support'
require 'yaml'
require 'ftools'

$mig_dir = 'db/migrate'
File.mkpath($mig_dir) if !File.exists?($mig_dir)

task :default do
Rake.application.options.show_task_pattern = //

namespace :db do
task :environment do
ActiveRecord::Base.logger ='.migrations.log', 'a'))

desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x"
task :migrate => :environment do
ActiveRecord::Migrator.migrate($mig_dir, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)

desc "Get the schema version number from DB"
task :version => :environment do
class SchemaInfo :environment do
abcs = YAML::load('database.yml'))

case abcs["adapter"]
when "mysql", "oci", "oracle"
ActiveRecord::Base.establish_connection(abcs)"db/schema_structure.sql", "w+") { |f| f db/schema_structure.sql"
raise "Task not supported by '#{abcs["test"]["adapter"]}'"

if ActiveRecord::Base.connection.supports_migrations?"db/schema_structure.sql", "a") { |f| f < max then n else max end

def next_migration_number
current_migration_number + 1

def next_migration_string(padding = 3)
"%.#{padding}d" % next_migration_number

def write_template(file_name, name)
contents = < :user_id do |t|
# t.primary_key :user_id
# t.column :user_id, :integer, :null => false
# t.column :first_name, :string, :null => false
# t.column :last_name, :string, :null => false
# end

# sample for self.down:
# drop_table :users

EOS"#{$mig_dir}/#{file_name}", 'w') do |file|

puts "Created migration file: #{$mig_dir}/#{file_name}"

Saturday, November 3, 2007

XBOX 360 D-Link 624

So the power went out... again... this time I could not reload my router settings from config (checksum error!) So I had to google my way back to XBOX Live...

have to open
udp 88
udp/tcp 3074
udp 53

The last one is very important... if I don't have all the above opened up for XBOX, my dlink router will actually reboot during xbox live tests (and fail of course.)

Back to fun.