Oct 06

Ruby is trading in your wife for a younger model?

Ruby, Tech 4 Comments »

Ted thinks that Ruby is a love affair.

He is close. I can see how it is like trading in your middle-age wife for a new younger model. However, I don’t think that in this scenario many are going back to their original partners.

It is probably more like we sometimes have to go back to see the mothers of our children, but it is a chore. We are thinking about our new partner even while we are with our old one (which DOES mean that we are better lovers in the old bed which is good).

There are always going to be other young models trying to get your attention, and even the old models are trying to do extreme makeovers to get us back there.

Or, maybe these are just languages? :)

Oct 02

LINQ 101 to Ruby 101 to Groovy 101

Groovy, Microsoft, Ruby, Tech 2 Comments »

Jon Udell posted some code that he was playing with to test out LINQ.

The code takes his blog format, and filters based on the XML, and some internal datastructures.

Then Sam Ruby ported it to Ruby.

For some reason I just ported it to Groovy:

def d = ["2005-09" : "September 2005", "2005-08" : "August 2005"]
def a = ["greasemonkey", "ajax"]

def rss = new XmlParser().parse("blog.xml")

def xml = new groovy.xml.MarkupBuilder(new PrintWriter(System.out))

rss.channel.item.findAll { item ->
d.keySet().any { day ->
t(item.date) =~ day;
} && a.any { tag ->
t(item.tags) =~ tag;
}
}.sort { x, y -> t(y.date) <=> t(x.date) }.each { i ->
xml.item() {
month(d[d.keySet().find { key -> t(i.date) =~ key }])
date(t(i.date))
title(t(i.title))
tags(t(i.tags))
}
}

def t(node) { return node[0]!=null ? node[0].text() : '' }

There are some uglies in there (especially the “node to text” pain), but I do prefer the native build syntax that we have in Ruby and Groovy, compared to nesting new XElement("item", ....). They could easily add more sugar to make that work on the .NET side of course.

The other interesting differences are that I didn’t use XPath in the Groovy version, and the lack of SQL like stuff. Here it is just method chaining. No need for a special orderby, you simply do a sort. Of course, the beauty of LINQ is its polymorphism across XML, SQL, etc etc.

Update: New Version

John Wilson took out his namespace aware XmlSlurper (to access dc:date vs. date), and a new builder syntax that handle multiple objects:

def d = ["2005-09" : "September 2005", "2005-08" : "August 2005"]
def a = ["greasemonkey", "ajax"]

System.out << new StreamingMarkupBuilder().bind {
mkp.declareNamespace(dc: "http://purl.org/dc/elements/1.1/")

new XmlSlurper().parseText(blog).channel.item.findAll {item ->
d.any{entry -> item.date.text() =~ entry.key} &&
a.any{entry -> item.tags.text() =~ entry}
}.list().sort{x, y -> y.date.text() <=> x.date.text()}.each {i ->
item([{month d.find{entry -> i.date.text() =~ entry.key}.value}, i.date, i.title, i.tags])
}
}
Sep 29

JRuby 0.8.2 released

Java, Ruby, Tech No Comments »

It was good to say that today JRuby 0.8.2 has been released:

This release fixes many problems. It also allows embedders to see Arrays and Hashes as java.util.List and java.util.Maps.

JRuby isn’t “there yet” of course, as you can see by the test failures, but it is getting there, and there has been an uptake of development on it which is great to see.

The day JRuby gets up to Jython levels, and then C Ruby levels, will be a great day.

Download JRuby

Sep 27

An example Ruby patch

Ruby, Tech 3 Comments »

Todd Huss posted his thoughts on dealing with patches to dependencies that you rely on, in response to my Tweaking on the bleeding edge: Ruby vs. Java.

Sam Ruby found an issue in Rubys XmlMarkup builder.

He put up a fix for this which is very clean.

I much prefer this to a diff’d file for patch to run. Isn’t it a lot cleaner to see? A patch is real running code.

Also, since you are able to alias :foo :saved_foo you can even do something and call that old method!

#!/usr/bin/env ruby

#--
# Portions copyright 2004 by Jim Weirich (jim.weirichhouse.org).
# Portions copyright 2005 by Sam Ruby (rubys.intertwingly.net).
# All rights reserved.

# Permission is granted for use, copying, modification, distribution,
# and distribution of modified versions of this work as long as the
# above copyright notice is included.
#++

require 'config/environment'
require 'builder'
require 'test/unit'

class Builder::XmlMarkup < Builder::XmlBase
def _escape(text, extra={})
result = text.
gsub(%r{&}, '&').
gsub(%r{<}, '<').
gsub(%r{>}, '>')
extra.each_pair {|key,value| result.gsub!(key, value)}
return result
end

# Insert the attributes (given in the hash).
def _insert_attributes(attrs, order=[])
return if attrs.nil?
q = {'"' => '"'}
order.each do |k|
v = attrs[k]
@target << %{ #{k}="#{_escape(v,q)}"} if v
end
attrs.each do |k, v|
@target << %{ #{k}="#{_escape(v,q)}"} unless order.member?(k)
end
end
end

class TestEscaping < Test::Unit::TestCase

def setup
@xml = Builder::XmlMarkup.new
end

def test_element_gt
@xml.title('2>1')
assert_equal '<title>2>1</title>', @xml.target!
end

def test_element_amp
@xml.title('AT&T')
assert_equal '<title>AT&T</title>', @xml.target!
end

def test_element_amp2
@xml.title('&')
assert_equal '<title>&amp;</title>', @xml.target!
end

def test_attr_less
@xml.a(:title => '2>1')
assert_equal '<a title="2>1"/>', @xml.target!
end

def test_attr_amp
@xml.a(:title => 'AT&T')
assert_equal '<a title="AT&T"/>', @xml.target!
end

def test_attr_quot
@xml.a(:title => '"x"')
assert_equal '<a title=""x""/>', @xml.target!
end

end
Sep 25

War of the Web: Revenge of the Dynamics

Ajax, HTML, Java, JavaScript, Lightweight Containers, Microsoft, PHP, Perl, Ruby, Tech, UI / UX, Web Frameworks 933 Comments »

As I was watching “24 hour party people” on DVD, I heard the main character talk about the ebbs and flows of the music business. He is talking about the scene in Manchester at the end of the 70’s, and into the eighties. Moving from Joy Division to Happy Mondays and New Order.

I think that we are in a new chapter for the web, and as is often the case, the wheel of time is repeating history for us.

There are a few dimensions to the current war though. They are on the client side (DHTML Ajax vs. simple HTML vs. Flash/PDF vs. XAML) and on the server side (Rails vs. Java vs. PHP vs. .NET).

Let’s start at the beginning.

Perl: Birth of CGI

Do you remember how the web changed as it moved from static HTML connected content to dynamic websites? That came about due to CGI, and how our nice web server would now fork off our programs to generate the HTML.

I remember my first CGI programs were written in C, and Scheme. I quickly moved on though, and found the beauty, and craziness of Perl.

I spent quite some time with Perl, trying to get by without writing too much NSAPI and ISAPI code (oops, I guess that core dump hurts the entire server?).

I really enjoyed the community at that time. #perl was interesting (some of the time), and CPAN became the holy grail. As soon as you thought you needed something, someone had kindly put that functionality up into CPAN. I even have some of my own modules hanging out there, and helped with others.

Over a short time period, we had developed some fairly rich web modules. We didn’t have to work with $ENV{’SOME_CGI_ENVIRONMENT’}, or STDIN or the like. Our framework abstracted all of that for us, and gave us a simple model. We lauched at the folks who generated html via methods such as b("whee") and we stuck close to HTML itself, allowing our design teams to simply open the html files and see what their stuff looked like. We even had the notion of components, and special tags that you could create. <$mytag name=”…” /> was nice because the name of the tag was the key for the framework to dynamically discover that functionality. No config files, or interfaces, in the strict sense. The coupling was based on a name.

In retrospect, life was pretty simple for web development, a lot simpler than some of the frameworks we have today!

But, we moved from Perl. CGI was not the nicest for our high load servers. It was crazy to think that we would fork a process for every little request that came in, and that a Perl interpreter would start up, load the program, do the work and then die off.

We naturally moved to solutions such as mod_perl, and that helped. It was so new though that it was buggy and we had a lot of problems. Some of the problems had nothing to do with mod_perl itself, but due to laziness and side-effects.

When you work in an environment like CGI you can be a very bad man indeed. If you don’t close something correctly, or don’t play totally nice with resources, baaaah who cares? The server is going to kill me in 2 seconds anyway, so I will get my job done and have him kill me. In mod_perl world though, these programs start to live longer, and they get fat and oily.

Java: No more stinking processes!

Remember the beginning of Java (Oak!). We were building applets, and feeling the pain very early on.

Servlets were the big thing though. We ported our Perl based framework over, and were able to see significant performance improvements at the time. Some of the team loved the change, others hated the verboseness and static typing.

The nice threading model that Java gave us was huge though, even with the poor JVMs back then (Microsofts was by far the best remember!).

This is when we moved from the world of Perl to having Java start to take over. That isn’t to say that there wasn’t competition. In the waters we saw the lurkers of ColdFusion, ASP, and the beginning of the PHP revolution. Java came up with JSP to compete with these tag based approaches, but it was the advent of the rich MVC style frameworks that really spurred everyone on.

In my opinion Java is still in the hot-seat, especially in the corporate world.

Preparing for the server war

The troops are being gathered. Strategies are being worked out. We are currently getting ready for a new battle on the server side.

What’s happening?

  • Ruby on Rails: Whatever you think about Rails, it has lit a fire under the server side web development community. Many have jumped on the bandwagon, claiming real productivity improvements. Some of the PHP converts enjoy a richer language, which is still nice and dynamic, with a framework that enforces clean MVC techniques. Some of the Java community are frankly a little bored of Java, and enjoy the new challenge. They also love the freedom of the language, and the fact that they now have just ONE stack to worry about. Will the Rails buzz keep growing? Will it be the Perl of Web 2.0?
  • Java: Java isn’t going down without a fight. Some argue that the problem with web development in Java is that it has been too complicated and heavy for much usage. I have personally called for the need of a common stack for Java, and people have stepped up to the plate. On one side we have companies that will certify a set of technologies (JavaServer Faces + Spring + Hibernate). Then we get frameworks taking on simplicity themselves (WebWork now embedding Spring). Finally we have initiatives like JBoss Seam, which is trying to combine the component models of JavaServer Faces and the backend. Seam aims to give you the power of the Java tier, but also giving you a simple productive environment. So, Java frameworks are rising to the challenge of Rails, and we will soon see how much of the success of Rails is Ruby, and how much can be duplicated in other platforms.
  • PHP: We can’t discount PHP. A lot of “serious engineers” (read: anyone who isn’t a PHP developer thinks they are serious) poo poo the PHP world. Yet, by all accounts, there is a LOT of PHP development going on out there. PHP has the advantage of being something written JUST for the web. Take a look at how Wordpress came along (PHP based blogging software) and in no time at all there were thousands and thousands of plugins that you could simply drop into your Wordpress system. Literally, you drop in a file and you are done. There are numerous PHP frameworks that are aiming to mimic, and compete with Rails, so we can’t forget about these guys. The question with the PHP community is: will it grow more into the enterprise, or will it be for script-kiddies.
  • .NET: Never discount Microsoft. ASP.NET keeps getting more productive, and it is hard to compete with their end to end story, which includes fantastic tooling in their latest Visual Studio. And, we get Avalon and XAML along for the ride, as well as the futures of C# 3.0 which takes a lot of ideas from the dynamic languages and puts them into a static structure (such as: var foo = new Bar(); and the relational/xml integration)

It is going to be an interesting couple of years, as all of these platforms mature, and take eachother on, trying to get mindshare!

Client Side: JavaScript is cool again

But what about Ajax? The battle for the client side is going to be just as hot as on the server. And they will even intertwine with eachother.

Firstly we have the big debate of how far Ajax is going to go. Is it a one hit wonder? or will it become a standard part of our toolbox and even just be called dhtml again?

As an Ajaxian, I obviously have my thoughts on this matter. But there is a lot of competition inside and outside of Ajax:

  • Flash/PDF: Adobe/Macromedia are a definitely force to be reckoned with. Flash is almost ubiquitous, and PDF is used everywhere. Now the companies are combined, what do they have in store for us?
  • Avalon/WPF/E/XAML: Microsoft announced WPF/E, which is a subset of XAML that will be ported on various platforms and available in many browsers. This means that you can build your rich application in the .NET set of tools, and have it run in Safari on Mac OSX. Impressive. When are we actually going to see this in a form that we can deploy to the real world?
  • HTML: How much do we want to work in the open (ish) world of HTML. A large group of developers do not want to jump into any monopoly, and will therefore want to stick to a more open environment. But, another set will just want to use the best tool to add business value. What will the split be?

JavaScript will play a big role in this war. JavaScript 2.0 offers big improvements, that many people will cheer for. Also, the same people who poo-poo’d JavaScript in the past have come to realise that it really is a great language. It may not be what they are used too (it uses prototype-based OO vs. class-based OO), but it is powerful and robust. There are some features missing, and a big question around libraries. JSAN and others are trying to build a CPAN for JavaScript. We also worry about the black box of the JavaScript VM in the browsers, and cross-browser bugs are truly real painful. Fortunately, frameworks like Dojo and Prototype are trying to help us out on that front.

We are also seeing that we need to take JavaScript from the former:

“That is just crappy code that the web dood View-Source’s and pastes into the web pages”

to the future:

“JavaScript also needs to be engineered, and is a first class citizen”

Thus we finally see more unit testing of JavaScript code, and professional ways of creating modules and namespaces for our code. We also see great advantages with features like E4X where XML becomes a native type.

JavaScripts increased popularity, thanks to Ajax (and Flash/ActionScript) has also drawn it into the server side. Mozilla Rhino gives you a quality Java-based approach, so why not use a cool dynamic scripting language for certain tasks on the server side? You don’t have to use JavaScript for everything, but it has its place, and that place is growing.

The Battles Join

This is where the battles are joining. We have JavaScript bleeding across the layers, and we have the need for server-side frameworks to support the new Web. It isn’t enough to generate simple HTML and be done with it.

Today’s frameworks need to be able to help us build Ajaxian components, and help us write this applications quickly and cleanly.

There are various directions that frameworks are going in here.

  • JavaScript Code Gen: Why not give you a simple macro that splits out the ugly JavaScript that you would have to write?
  • JavaScript Framework Code Gen: Spitting out low-level JavaScript is too much work. Many frameworks are writing on top of a higher level JavaScript framework like Dojo or Prototype. Now the code-gen is less, and you get the benefits of the rich functionality, browser compatibility, and visual effects available from these frameworks.
  • Tools and Widgets: Should developers even care if a piece of their page is Ajax or not? Some frameworks give you drag and drop editors that let you setup widgets or components. Some happen to be ajaxian. Some are not. Who cares?
  • Markup based: A lot of frameworks are giving us markup based solutions. That is one of the strengths of Microsoft Atlas, not the fact that they added support for $() etc. Are we going to want to build using markup or via programatic APIs?

Conclusion

It is hard to predict the winners of the new battles, and the losers will not die off totally, but it is an exciting time to be watching web development. The dynamic languages of Ruby, JavaScript, and PHP are making a big run, and people are realising that they aren’t just cheesy scripting languages that can’t be used. It’s time to take them serious.

We are going to start really working out what makes sense for usability on the web with rich interfaces. And, at the same time we will get simpler and simpler backend tools to make the generation of rich web experiences easier and easier.

I am looking forward to seeing this battle!

Sep 23

Tweaking on the bleeding edge: Ruby vs. Java

Java, Ruby, Tech 54 Comments »

Something clicked recently, after I compared a couple of scenarios of recently life.

The two cases have to do with extending your framework, especially on the bleeding edge.

Extending Java: Hibernate and XDoclet

Ben Ellingson has been working on an Adigio project that is pushing the edge with Hibernate 3 and XDoclet 2.

To get things working, both Hibernate 3, and the XDoclet 2 plugin had to be patched, resulting in our own builds.

Luckily, there are JIRA issues filed, and in the Hibernate 3 case a fix seems to have already been put in.

Extending Ruby: Rails

In another life, a Rails project needed to alter the way Rails did something. Rather than having to have a special jar file, or dependency, or anything like that, all that needed to be done was to re-open the classes that needed tweaking, and change the methods!

Having a system that allows open classes is a pleasure when it comes to making changes like this. So much cleaner!

Sep 22

Ruby on Ruby on Rails

Ruby 1 Comment »

It is good to see Sam Ruby “Prototyping Intertwingly on Rails”.

He loves porting his blog software to every framework under the sun, and this time he talks about his experience porting to Rails.

I still have much to explore: localization, logging, redcloth or equivalent, caching, spell checking, trackbacks, automated excerpts, Atom publishing protocol, comment throttles, etc., etc., etc.; however at this time I see nothing that will likely get in my way – to the contrary, I see quite a bit of things that I can build upon in Ruby on Rails.

It seems like a positive experience :)

Sep 13

Tagging Object Model with Rails

Ruby, Tech No Comments »

Dema has created a nice acts_as_taggable mixin that allows you to add metadata tagging to your ActiveRecord object model.

Nice and clean.

class Photo < ActiveRecord::Base
acts_as_taggable
end

photo = Photo.new

# standard tagging with a string
photo.tag 'brazil rio beach'

# tagging with an array
photo.tag ['south america', 'soccer']

# tagging with a different separator
photo.tag 'beautiful women, babes, hot chicks', :separator => ','

# tagging with a Proc separator
photo.tag '2001..2005', :separator => proc { |s| eval(s).to_a }

# suppose your tags_photos join table has attributes
# and you want to set them while you tag
photo.tag 'samba', :attributes => { :tagged_at => Time.now }

# Let

Sep 10

Sam Ruby’s Case for Dynamic Languages

Ruby 1 Comment »

Sam Ruby gave a keynote at FOSSSL on The Case for Dynamic Languages.

He details the past to the present, and then shows why he thinks this is all good :)

He happens to use Ruby as the scripting language ;)

A lot of the ideas come from Jim Weirich’s 10 Things Every Java Programmer Should Know About Ruby, which is something everyone should read.

He hits the nail on the head with items that make me cringe to be without elsewhere.

It is the combination of all of these items that makes Ruby elegant in my book.

Aug 16

VitalSource Bookshelf: “It’s like iTunes for eBooks”

Ruby, Tech No Comments »

Good ole Mike Clark and James Duncan Davidson have been beavering away on a Rails-served application.

They finally went public.

Cool to see this different kind of application.

I am a little concerned with not having just a simple browser interface (ajaxian?), as already I feel differently changing gears from:

Cool. Will check this out quickly

to:

On man, I have to install something?

Good stuff guys.