Another neat trick


Another sweet thing, that I’ve actually wondered idly about from time to time, is how you could get “provided?”-style parameters in Ruby. If you have used a language with this functionality, like Common Lisp for example, you know that this can be highly useful.

And just the other day, while reading Hal Fultons excellent (but poorly proof read) The Ruby Way (2nd ed) I found the way. Ergo, it looks like this:

def foo(bar, baz = (baz_provided=true; 42))
p [bar,baz,baz_provided]
end

foo(13,47)
foo(13)
foo(13,42)

This will provide the output:

[13, 47, nil]
[13, 42, true]
[13, 42, nil]

which illustrates the usage pretty well. The magic, of course, lies in the fact that default parameters are evaluated, just the same as anything else. You could do some very crazy things inside that default value specification if you wanted. Anyway, just a nugget.



A very small Ruby method


I haven’t had much time or inclination for blogging lately. Not much happening at the moment, actually. But I came up with one small thing I wanted to document. Just a practical thing for certain situations. Basically it’s a with-method, that works fairly well. It’s nothing magical and the trick is basic. It’s more or less an alias, actually:

module Kernel
def with(obj = nil, &block)
(obj || self).instance_eval &block
end
end

with("abc") do
puts reverse
end

"abc".with do
puts reverse
end

As you can see, insstance_eval can be used like JavaScript or VB’s with. This is nice for the simple reason of documentation. I find this usage much easier to read and understand than most usages of instance_eval that I’ve seen.

So, that’s it for today. I’ll probably be back soon with some recent JRuby developments too.



The Curse of Camping: The story continued


It’s time for another installment in the Camping saga. The last time I wrote about 1.5, and some things that didn’t work properly in JRuby. This time, the announcement is once again this: Camping 1.5 works in JRuby. It works very well, actually. To show this, I will make it very easy for you to follow along in how to achieve it.

First, have JRuby installed, either from trunk or version 0.9.2 if that version has been released when you read this.

Secondly, gem install ActiveRecord, Camping and all their requirements.

Thirdly, gem install ActiveRecord-JDBC.

Fourth, put you MySQL JDBC driver on your classpath (or any other JDBC driver you care to use).

Download blog.rb from the Camping examples here. Modify it by adding the line

require ‘jdbc_adapter’

after the other require’s at the head of the file.

Finally, you need to create a database file, since JRuby doesn’t support the standard choice of camping (which is SQLite3). To do this, create a file that’s named (on Linux) $HOME/.campingrc, or on Win32 %APPDATA%/Campingrc. (%APPDATA% is on most Win32-boxes something like c:\Documents and Settings\olagus\Application Data). This file should contain your database definition in standard ActiveRecord fare. My example looks like this:


database:
adapter: jdbc
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/blog
username: blog
password: blog

Of course, you should use your own individual settings and driver information.

Camping does only require that the database exists. It will add migrate the blog application automatically, and as soon as you’ve started the application by doing:

jruby $JRUBY_HOME/bin/camping blog.rb


it will listen on 3301 and be ready to serve an example blog application.

Have fun and enjoy!



Freaky freaky Sandbox has come to JRuby


I would like to announce the first release of JavaSand, which brings _why’s spooky Sandbox to JRuby.

For those of you who hasn’t played with Sandbox, it allows you to create a new area for executing Ruby where you have control on which classes are loaded and available. It’s good for lots of things. It can be made very secure, for example. Or you can load several applications in different Sandboxes and they won’t interfere with each other in any way.

Now, in MRI, this is done through some fairly involved swapping of symbol tables. In JRuby things are quite a lot easier. Actually, it’s only a matter of loading the right classes and remove all methods not allowed.

So, how do you get to use sandbox from JRuby? First of all, you need a special JRuby, that can be checked out and compiled from svn://svn.codehaus.org/jruby/branches/sandbox. This functionality will come to regular JRuby to, but not until after 0.9.2 has been released. So, when you have this special version downloaded, you just install the RubyGem called javasand, and you’re set to go. If you would like a fun example to try out, you can download sandbox_server directly from _why’s code repository http://code.whytheluckystiff.net/svn/sandbox/trunk/bin/sandbox_server. To run it:

jruby -rubygems sandbox_server

and you can connect to your machine at port 5000, press enter, and you have an IRB session. Wow!

So, the project JavaSand is part of JRuby-extras and the source is in that repository, at RubyForge! Go ahead and test it now!



The FINAL OpenSSL post?


Possibly.

I’ve checked in all functionality I will add to OpenSSL support in JRuby at this point. Of course, there will be more, but not concentrated in a spurt like this. Tomorrow I will modify the build process and then merge everything I’ve done into trunk.

Let’s back up a little. What have I accomplished? This: All OpenSSL tests from MRI run (except PKCS#7). That includes tests of SSL server and SSL client. Simple https-request also works. This is sweet. Everything else there is tests for in Ruby works. But… this is also the problem. Roughly half of Ruby’s OpenSSL library is not tested at all. And since the current OpenSSL initiative from my part is based on tests, I haven’t done anything that isn’t tested for.

So, some things won’t work. There is no support for Diffie-Hellman keys right now, for example. Will be easy to add when the time comes, but there isn’t any testing so I haven’t felt the need.

The only thing not there, as I said, is PKCS#7. That was just too involved. I’ll take care of that some other time, when someone says they want it… Or someone else can do it? =)

So, what this boils down too is that JRuby trunk will have OpenSSL support sometime tomorrow. Hopefully it will be useful and I can get on to other JRuby things. I have a few hundred bugs I would like to fix, for example…

Oh yeah, that’s true. Tomorrow will also be YAML day. I’ll probably fix some bugs and cut a new release of JvYAML. It’s that time, the bug count is bigger than it was, and JRuby needs some fixes. So that’s the order of day for tomorrow. First OpenSSL and then YAML. Any comments on this, please mail or comment directly here.

G’night.



JavaForum: last night


And now it’s over. Last night was the last presentation for me in a while. Over 200 people where there and the place was packed. I must say I’m very pleased with how my part went. I felt really good, much flow and the demonstrations went perfect.

Rob Harrop and Adryan Colyer talked about Spring 2.0 and AOP. It was fairly interesting, but since I’ve done some work with AOP before I felt that the level was a bit to introductory. No real meat.

JavaForum announced a day focused on Java in January, with four tracks and some great speakers. Looks very nice.



JRuby in Stockholm, JavaForum on Tuesday


On Tuesday it’s time for the big JavaForum in Stockholm. I’m looking very much forward to it and have spent some hours today sharpening the presentation. Of course it will be very interesting to see Rob Harrop too.

In other news, something quite exciting is probably just around the corner…



JRuby presence at JavaOne 2007


The call for papers went out two days ago, and we already have several good proposals in line that will be submitted to JavaOne. Hopefully we will get many interesting talks this year. As far as I know, there are at least 5 JRuby-specific proposals submitted, and there will hopefully be more.

I am sending in two different proposals this year, one about JRubyEE (it’s called “Agile Enterprise Development with JRuby”). That will be interesting to do and I really believe there is much for Java enterprise development to use in JRuby.

The second proposal is for a BOF about several JVM languages called “Lambda on the JVM: JRuby, Jython and Lisp”. I’ll talk about how to utilize different JVM alternative languages to do interesting and useful things. Since this is my venue, I will specialize on languages that are derived from Lisp (implicitly or explicitly). (I include both Ruby and Python in this category).

Hopefully at least one of the proposals will get through.



The obligatory meta-blog post.


So, this is post number 100, since I started blogging. And that’s almost exactly one year ago. Isn’t that interesting? I’ve been writing something here each 3.65’th day. Often it’s been uninteresting, very often indifferent, but sometimes I have manage to write something that people liked. So, I’m happy about this small anniversary.

So, I plan to do what almost every blogger do at least once: turn the writing process inwards and investigate why people blog, why the person in question blogs, and perhaps, if we are really META, why the current blog post is actually written. Hopefully I’ll know why I’ve written this by the end of this post. Otherwise, it’ll probably be something indifferent or boring.

Let’s begin. “Why blog?”, the general question. When I started out, I had no real goal. I knew that I wanted to get better at technical writing and that was largely it. I hoped I would get some new contacts and interesting discussions but the base reason was for the writing ability. As such, I wouldn’t have to write public; I could hone my writing skill in my basement and never publish anything to the (sometimes scary) scrutinizing eyes of the public. But that’s the problem. If I didn’t publish it, I wouldn’t have as much incentive to become better. Now I absolutely have to get better at writing, otherwise I will feel ashamed about the filth that will for all time be remembered by Google.

That’s reason number one. The second reason, that I’ve come to realize this last year but didn’t know when I started out, is that I learn by writing about a subject. If it’s a technical topic I basically have to research the topic quote thoroughly, so I won’t look like a fool. And that is really good, that is incredibly good. I have learned very much by writing about things I find interesting. The first learning when I research and think about what I should write, and the second learning when people read it and comment and correct me.

Reason number three can be thought of as hubris. I believe that some of the things I know can be useful for other to know, and I believe that some of the problems and bugs I uncover in open software should be documented somewhere so others won’t have to go through the same bug finding escapades as I did on that subject.

And here’s where we come to the point. The point is that if you’re working with software (or with anything where information sharing is important, really), you should write about. Because the writing benefits you and it benefits me. If you find a problem, write about it. Doesn’t matter if someone actually reads it or not, for the most important aspects of writing is about yourself.

Steve Yegge wrote about this in his “Blog Or Get Of The Pot”, and also in one of this older blogs (“Why you should blog” I believe it’s called).

Coda: Why did I write this post? To make it clear in my head why I blog. To make it obvious and explicit why I do it. So that’s what this post is about, and it’s a recursive post since it talks about itself. I’m satisfied.



Some notes from the Stockholm Rails meet


Last night (Wednesday) about 35 developers with an unhealthy interest in Rails met up in Stockholm, at Valtech’s offices, to share some experiences and talk shop.

It was very interesting and fun to meet people I can relate to. We ended up talking programming languages for a few hours after the main event ended.

Peter Marklund did a presentation on a CRM system in Rails, and Christian and Albert from Adocca talked about caching, and handling a Rails application that needs to scale into the millions. Nice stuff.

Last, I did an improvised presentation on LPW, a small Rails application that is fed its main data through Web Services instead of ActiveRecord. I believe it went fairly well, even though I had no slides and almost no preparation… =)