Archive for the ‘Ruby’ Category

Use Burlap to build a simple Java-Ruby bridge

Tuesday, March 13th, 2007

Yes, I know, there is this great tool JRuby which will hopefully be an essential
part of the JDK in the near future, and yes there is
REST where Restlet seems to be the most promising framework,
but all I wanted is to speak with a very simple Java Interface from a Rails application.
So I thought there must be an easier way, but it has to deal with ongoing changes to the interface.
The answer for me was the Burlap protocol which you get with Spring for free.
All you have to do is to expose your Java bean with the Burlap exporter:


 <bean id="fooService"
 	class="com.agelion.server.impl.DummyService">

 <bean name="/FooService"
 		class="org.springframework.remoting.caucho.BurlapServiceExporter">
     <property name="service" ref="fooService"/>
     <property name="serviceInterface"
     			value="com.agelion.server.FooBackendService"/>
 </bean>

. Thats it for the Java side. Burlap is a very simple protocol where you dont map classes, what you get is a map with key, value pairs. But thats fine for me.

But also the Ruby side is easy to implement. I am still a newby with Ruby but that was relativly quick done (though its still not as easy to get information like you get for J2EE for instance, where you get too much information).
Because the Burlap protocol just supports the POST method you need the low level Ruby methods for Http:

Net::HTTP.start('localhost', 8080) do |request|
    response = request.post('/backendservice/FooService',
    	'<burlap:call><method>myMethod</method></burlap:call>')
 

And here we go. All you have to do is to parse this XML:


 vals = []
 XPath.each( Document.new(response.body),
 				"//burlap:reply/list/map/*")
	{|p|  vals << p.text }
 

And as I said, what you get are key-value pairs with the exception of the first element which descripes the original type (classname).
I converted it to a map skipping the first element in the array:

 @bashvals = {}
 1.step(vals.length-1, 2)
    { |i|  @bashvals[vals[i]] = vals[i+1] }

No RailsConf for me

Sunday, August 20th, 2006

Too bad. A new project popped up on the horizon, and its most important deadline is September 15th. The choice was either the project (and therefore earning money) or RailsConf Europe in London.

Well, see you next year, or at RailsConf Germany.

One Month on Rails

Thursday, June 15th, 2006

The last weeks have been rather exciting for me. Got to work a lot on a Rails project, and if it’s possible, got to love Rails even more. Got to play with a lot of its features, and there’s more to come. RJS templates are especially great things. Having the client-side code on the server-side in the server’s language is excellent stuff. O’Reilly just published a nice PDF book about RJS. Good stuff and well worth checking out.

Testing with Rails rocks. It takes some coding before I even turn to the browser for testing the GUI stuff, and then mostly to test the interaction of the implemented things. The rest is nicely implemented as tests.

Anyway, it’s been a lot of fun, and it’ll keep me busy for some time still. I don’t miss Java at all, implementing stuff in Rails is way more helping in focussing on the future in general. It’s great to do something completely different from what I did for the last two and a half years. Not that I completely dislike Java now, but it’s just great to completely delve into something new to learn it as best as possible. Having good Rails experience might turn out to be a good investment ;)

So far, freelancing has been sweet for me. Got my own office, and summer’s finally arrived in Germany. Which is all not to say that freelancing might turn out bad, I’m very aware of that, but so far I still think it was the right thing to do for me.

What sucks is that my MacBook Pro 17″ arrived last Friday and was only 10 minutes after unpacking ready to be declared DOA (dead on arrival). New one’s been ordered, but hopefully I don’t have to wait for another four weeks. I got to play with it for some time, and it just kicks butt. Spring builds in seconds, Eclipse starts in seconds, too. Just awesome. Amazing display, great sound, screaming fast. Oh, well.

Technorati Tags:
, , , ,

Fun with Fixtures

Thursday, May 18th, 2006

Fixtures are a neat feature of Rails. However there’s a small caveat that pops up when your classes don’t follow the conventions and use a table whose name is not a pluralized version of the class name, e.g. the class is `HorseShoe` and the table name is just `shoes`. The corresponding fixture would have to be name shoes.yml and included in the test case with `fixtures :shoes`. Now as soon as you try to access one of the fixtures through `shoes(:iron_shoe)`, the fixtures code will try to look up the class name to instantiate it. With the default conventions it will look for `Shoe`. To avoid this, I found one way (probably not the only one) to tell the fixtures code what class to use. After defining your fixtures you can tell it which class to use for which fixture:

class ShoeTest < Test::Unit::TestCase
fixtures :shoes
set_fixture_class :shoes => “HorseShoe”

end

The call to `set_fixture_class` will merge your custom table-class mapping into its own, and whenever you’re trying to access one of the fixtures, it will default to your configured class instead of the singularized table name.

As I said, this probably isn’t the only way, but it is one way I found while browsing through the fixtures code.

Ruby on Rails - Looking past the Hype

Monday, May 8th, 2006

For about two years now, Rails has been in our lives. It has arrived sort of violently and with a lot of buzz. Some say it’s all bullsh**, others love it to death, just like with every new technology. Both sides have their points. But the important thing is, does Rails have a future?

Right from the beginning. Rails has been dismissed as a toy and bitched about, especially by the Java community. Is it fear? Is it ignorance? I’m not quite sure. Maybe it doesn’t even matter anymore, because those voices got quieter over the last few months. And you don’t even have to look far to find some influences. Although Rails as a framework has its downsides (surprise!), it quickly influenced a lot of other frameworks, including Trails, Grails (I’d suggest finding new names), CakePHP or TurboGears.

Concerning the downsides, it seems that the first thing people looking for is negative things. Rails can’t do this, Rails can’t do that, you heard it. That’s an easy and comfortable way to do things. Pointing out the obvious is simple, suggesting an improvements or asking why it’s been done that way is another. Granted, David is quite picky about how things are done in Rails, but hey, you can always write a plugin. What I’d wish for are sayings like “Why isn’t this done that way, I/we could achieve <insert-some-cool-and-money-making-feature-here> if it were”, and not “I hate Rails, because it does xyz not the way <insert-some-other-framework-here>”. There’s always a reason, and granted, if it’s not implemented the way you like it, there are two possibilities, either you’re looking at things from a too technical point of view, or you just shut up and move on.

In technical terms Rails is still quite young, having reached v1.0 last summer. Does that mean anything? Probably not, since all technologies add up to what is Rails are only glued-together components that build up on existing knowledge and technology. That’s what surprises me. People bitch about it although it’s not really anything new. They use similar frameworks in their applications, be it Hibernate, WebWork, EJB and whatnot. Granted, Rails as it is has been a little over-hyped, for that matter. But in the end, it’s not ground-breaking news (which, of course, nobody said it would be ;).

So set aside all the good and the bad, the question still is, has Rails already reached its tipping point? Has hype become mainstream? From a technological point of view, I’d say yes. Rails as a combination of good practices and existing technologies (as in ways to do it, not frameworks) is definitely on the right side of the bell curve. The conservatives already have taken over what’s proven to work. But Rails as a framework is what I’d say still a tool for early adopters. I don’t mean that in a negative way. Rails is being adopted by more and more people, who are curios, or sick of other, more heavyweight ways to do web development. Some people already are convinced and do all their web development with Rails. Good on them! Others do it for fun (and maybe a little profit). For the time being I see myself as one of the latter.

The wide adoption of Rails is still to come, if at all. I’m not being negative here, but it’s a possibility that a new technology will never be adopted by a broader audience. Some would probably love to see Rails fall, others to see it rise to the tipping point. It’s hard to predict when the tipping point will be reached, but what isn’t? I’m not sure, if I’d fully count on Rails for starting or running a business, but I’d definitely include it in my portfolio. Don’t get me wrong, I’m keen to see where Rails will be going, but for the moment I’m planning to stay with at least one leg on the J2EE side of things. Although I gotta say, sometimes I feel a little confused, when I come back from some Rails hacking, back to working on Java-code. It’s all so complicated and unexpressive ;).

So it’s not up to me to decide what the future has in stock for Rails, it’s up to the masses. If they won’t get acquainted with the Rails way of things, and start using it on more and more projects, Rails will not hit the mainstream. If that’s good or bad is up to you. I’ll be there celebrating, if it happens. And for the time being, I’ll make a living of both, J2EE/Java and Rails.

And meanwhile, you might wanna register for RailsConf Europe. See you there.

Technorati Tags:
, , ,

Second Edition of a Rails Classic

Thursday, May 4th, 2006

Why, what’s that in my mailbox? Oh yes, it’s the first beta of the shiny and new second edition of “Agile Web Development with Rails”. It’s in the works of being updated for all the glory features of Rails 1.1, including RJS-Templates (which are awesome, by the way), migrations, Capistrano, etc. The beta program will work in a way that the first edition will be transitioned to the second page by page. The second edition is already at almost 600 pages. The chapter on installation got a complete overhaul, including more detailed database setup, available editors, stuff like Instant Rails and Locomotive. Will be great to see it growing, and slowly transition into the second edition.

And now the covers features a picture of one of my favourite hobbies, too ;)

You can buy the book as a beta PDF now, or with the dead-wood edition when it’s finished. If you desperately need the paper edition, you can buy the transition combo including the first edition as paperback and the second edition as PDF.

In other (slightly older) news, the Radium CMS has gone public.

Technorati Tags:
, ,

Creating XML with PHP and Rails

Friday, November 18th, 2005

Over at the Big Nerd Ranch they’re showing how to create XML with PHP DOM. You might wanna have a look at it and compare that to the simple Rails approach:

— teams.rxml —

xml.instruct! :xml, :version => “1.0″, :encoding => “UTF-8″
xml.teams do
xml.team do
xml.name “Atlanta Braves”
xml.stadium “Turner Field”
xml.league “National”
end
xml.team do
xml.name “Chicago Cubs”
xml.stadium “Wrigley Field”
xml.league “National”
end
xml.team do
xml.name “Baltimore Orioles”
xml.stadium “Camden Yards”
xml.league “American”
end
end
— end —

When speaking of PHP5, they could at least have pointed their readers to the SimpleXML library. It’s not as pretty as the Rails solution, but it’s a start.

First Steps on Rails

Friday, October 14th, 2005

So you hear a lot of stuff about the great productivity of Rails. You see the video, go throughsometutorials, buy the first book about Rails, and you’re stunned about how Dave Thomas cranks out feature after feature. In only a few hours, you have something put together that’s close to a small online shop. You read on and are amazed about the features of Rails. Those guys seem to have thought about everything a web developer could ever need (well, for most cases, but think of the 80/20 rule). So what’s the great deal about Rails? Obviously there’s the beauty that Ruby is. A slick little language that brings everything you need (well, except good Unicode support, but they don’t seem to like Unicode at all in Japan). It’s the complete implementation of DRY. It’s the tight integration of unit testing. The latter is amazing. It’s so darn easy to test everything from the model to the controllers and even parts of the web application. Fixtures (fixed test data creation) are built in and the database is clean on every run. What more could one wish for? It certainly isn’t perfect, but as James Duncan Davidson put it, the point is to get you into high gear fast and deep into the water, and all that without much hassle.

This sounds as if Rails is the perfect match for the web developer’s every-day-problems isn’t it? It did sound/read like that to me. So what’s the next step? Certainly starting to work on your very first Rails application.

At first it’s all a little bit weird, no matter what background you have. I shied away from web development after developing with PHP for 3 years. I don’t want to start a discussion about PHP here, but truth is you get somewhere fast. But for a price, be it code quality, maintainability, you name it. Java on the other hand is very talkative. You need quite some code and configuration files to get somewhere. I haven’t had the joy to develop with Struts yet, but I maintain the build process for an app including web components built with it. And it’s no joy to see how many lines of code it takes to get that web application going.

So there are mixed impressions here. There’s clean code, separated nicely, and there’s less code, well structured from the very start, and all of that by convention. Everything feels natural, nothing is forced on the developer, instead conventions are used throughout the framework which are something like a recommendation, not an enforcement (hello legacy schema). It takes some getting-used-to, but once you’re there, it’s a great pleasure to work with it. There’s loads of documentation available, a well filled Wiki, and a nice community waiting to help. I had to get some rocks out of the way, especially when it comes to testing, but that’s the difference between the stuff you read about and the practical part, where you’re taking your first steps into the new world by yourself. The best thing about Rails is that it doesn’t feel like developing a web application. You code a little, test a little, and do some HTML, or AJAX if you will (a breeze with Rails). But altogether it feels like developing a normal application (at least it does for me). It’s new, it’s exciting, it’s worth giving it a shot, what ever preconceptions one might have (that goes for every technology, by the way).

So where does the spoken-of productivity come from? As you’re diving into Rails, that’s not easy to say. But as you learn to hold your breath for a longer time under water, and get to know the facets of Rails in more detail, things definitely come along easier. It’s not the scaffolding (although that can give you a nice head start), it’s the stuff Rails does for you (meta programming galore), it’s the easy testing facilities, and, last but not least, the wonderfulness and simplicity of Ruby.

Rails definitely did something for me. It made me realize that developing for the web can be a pleasure. And that’s A Good Thing™. Now I’m looking forward to the adventures to come with Rails.

ONLamp just put up a nice overview about Rails and its components.

Pragmatic Fridays

Monday, September 26th, 2005

Well, it’s not really Friday yet, but the Pragmatic Programmers released the first book of their new series of micro-books, called Pragmatic Fridays, anyway. Dave Thomas talked about them in the Ruby on Rails Podcast #2. They’re only about a hundred pages in size, PDF only (DRM-free, of course and cost between 7.50$ and 10$.

The first one is “Rapid GUI Development with QtRuby”, not surprisingly dealing with GUI development with the great Qt library and Ruby using the QtRuby bindings. Check out the excerpt. Perfectly suited for display reading which I tend not to like otherwise.

In the future we’ll see more Ruby-related micro-books

Great stuff guys, and keep ‘em coming!

Germany has a Rails User Group

Saturday, September 24th, 2005

Finally, someone (someone being Jan Krutisch and Ramon Wartala) took a step forward and decided that it’s time for Germany’s very own Ruby on Rails User Group. There is a Weblog and a Mailing-List available for a start. Future plans include a network for German Rails-Users, a Wiki, and maybe other useful stuff too, like local meetings, talks, you name it. Come on in and join us to help spread the word about Rails in Germany.