HotCocoa!

The latest MacRuby with HotCocoa is amazing! Finally a way to write OS X applications without having to learn Objective-C.

require 'rubygems'
require 'hotcocoa'
include HotCocoa
application do |app|
  win = window :size => [100,50]
  b = button :title => 'Hello'
  b.on_action { puts 'World!' }
  win << b
end

Daemons

Daemons provides an easy way to wrap existing ruby scripts (for example a self-written server) to be run as a daemon and to be controlled by simple start/stop/restart commands.

Dependency injection and why Java sucks

Jamis Buck reminds me why I dislike Java in his Net::SSH revisited post.

It’s not that Java is bad. It’s quite a nice language with a good solid foundation of available classes and libraries. No, the problem is all the frameworks and design patterns you need to use to be able to work at the same level as what come as default in a language in Ruby.

Dependency injection is a good example. I remember then it was the latest hype in Java-land a few years back. In Java it helps you write modular code without tight coupling between the components. And that’s a good thing, right?

However, in Ruby this is a non-issue. You can accomplish the same in Ruby without using a framework and extra libraries.

This is what I dislike with Java. It holds me back! With Ruby I feel I have an incredibly amount of power available at my finger tips. I can concentrate on the fun thing; solving the problem at hand. No need to fuss around with frameworks, support libraries and what not, just to have the ground to build upon.

So to summarize. Ruby makes you happy. Ruby makes you smile. Ruby gives you all you need.

DRb with jruby and normal ruby

If you get weird errors then your normal (i.e. using the ruby implementation in c) ruby program tries to access a DRb resource shared from a DRb server running on jruby try to upgrade to the latest jruby release.

I got very mysterious errors with jruby 0.9.8 but the errors went away after I upgraded to the 1.0 release.

Oh, and furthermore don’t test your DRb stuff with a method named ‘test’. It will work with normal ruby but with jruby you’ll get a ‘trying to access private method’ error. It seems like jruby defines a private test method on objects for some reason and you end up with a conflict.

JRuby

I’ve played with JRuby a bit lately and it’s quite marvelous. I wanted to use a library that was only available in Java but the rest of the code I had written was in ruby, jruby then magically solved my problem and saved me a bunch of time. Super!

Net::HTTP.post_form

Maybe I didn’t get how it works but I can’t get the Net::HTTP.post_form method in the ruby library to post to a URL like this, http://foo.bar/foo.php?q=search, i.e. a URL with GET query parameters.

The post_form method takes an URI instance (which you get with something like u = URI.parse(“http://foo.bar/foo.php?q=search”)) and then the code works like this:


def HTTP.post_form(url, params)
  req = Post.new(url.path)
  req.form_data = params
  req.basic_auth url.user, url.password if url.user
  new(url.host, url.port).start {|http|
    http.request(req)
  }
end

You see the use of url.path there then creating the Post object? That won’t include the ?q=search part of the url.

I’m not sure if this is the way it’s designed to work but I haven’t been able to figure out a way to mark any parameter I send in to the post_form method in params as a GET parameter. If that was possible this would work fine but for now I’ve settled on the below piece of code:


def my_post_form(url, params)
  req = Net::HTTP::Post.new(url.request_uri)
  req.form_data = params
  req.basic_auth url.user, url.password if url.user
  Net::HTTP::new(url.host, url.port).start {|http|
    http.request(req)
  }
end

which will do for me until you all tell me how stupid I am and give me a better solution 🙂