Calling Methods During Class Definition

9:38 PM EDT Thursday, April 17 2008

One of the features I like most about Ruby is the ability to execute code during the definition of a class. Here's a simple example:

class Foo
  puts "hi"
end

A more useful example is that you can call a method and self is set to the class you are defining.

class Foo
  def self.whoami
    puts "You are #{self}"
  end
end
class Bar < Foo
  whoami
end

This prints You are Bar. This is a feature that isn't common to most languages, for example, you can't do it in PHP, Python(I don't think, Pythonistas jump in there if I'm wrong), Java or even Groovy. The reason why this kind of method is so helpful is this how you can write code that writes code. This is the closest thing Ruby has to Lisp macros. You see this used in Ruby in with the attr_accessor method and in Rails with many methods, belongs_to and has_many being the most obvious examples.

This allows you to define the metadata about a class in the most DSL-like syntax. It would be really great to see this kind of thing in Groovy.

Posted in  | Tags Python, PHP, groovy, Ruby, Java | 0 Comments

The Edge of Innovation

9:24 AM EDT Friday, April 11 2008

If you are a programmer that deals with web applications and you keep up with the latest trends, then there is no doubt you will at least have heard of Ruby on Rails. You might be at the level where you have read about Ruby on Rails and played with it a bit, but you really haven't immersed yourself the Ruby/Rails way. Maybe you know how to build web applications with Java, .Net or PHP, and you think "Rails is nice, but there's nothing Rails can do that you can't do in X".

If you are in this camp, it may be because you aren't willing to adapt your ways to Rails. To really understand the benefits of Rails, you have to not only learn Rails, but learn the best practices followed by good Rails programmers, like Skinny Controller, Fat Model, REST and Behavior Driven Development. You don't see the true benefit of Ruby until you start to fully embrace concepts like these. The only way to learn concepts like these is to read blogs, listen to podcasts, talk to other Ruby programmers at work, user groups and conferences and most importantly, you have to actually write code that reflects what you have learned. You must put aside your preconceived notions about what is right and what is wrong and surrender to the flow. You must unlearn what you have learned.

So if you haven't experienced this transformation first hand yet, and someone asks you "What is the biggest advantage to Ruby on Rails", I would be willing to bet your answer would be productivity. This is the way I think many people involved with technology, who don't fully grasp the Rails Way, perceive Rails. They believe, with some cynicism, that because of the dynamic nature of Rails, you can develop applications faster. They'll also probably say the downside is that Rails can't scale.

But anyway, I'm hear to say that productivity is the most over-rated benefit of Rails. The real advantage to Rails is that it is written in Ruby, which is a very powerful language that will change the way you think about programming. It's funny, I've thought to myself a number of times about how interesting it is that Ruby fundamentally changes the way you think about programming and that "Thinking in Ruby" would probably be a great book. But ironically, Bruce Eckel, the author of the "Thinking in..." line of programming books, seems to be happy with Python and not willing to give Ruby a chance. Who knows, that article is a few years old now, so maybe he's changed his attitude towards Ruby since then. I know mine has.

It's hard to quantify the advantage that "Thinking in Ruby" brings. The simplest way I can state it is that you will look at problems differently and come up with better solutions, solutions you may not have thought of if you were programming in other languages. The way I support this claim is by looking at some of the web application development innovations that have come out of the Ruby community.

The first is Rails itself. Rails has been copied, ported, attempted to be ported or talked about being ported to almost every other mainstream language you could think of, including Groovy, PHP, JavaScript, Perl, Java and .Net. This phenomenon is unique to Rails, I can't think of any other web application framework that can say that. If not the whole framework itself, parts of it such as convention over configuration, migrations and embracing REST have influenced the way web application development is done in almost every language.

Another example is HAML. HAML is a truly new and different take of the problem of generating HTML from a combination of dynamic code and HTML. It is a new idea and it has been ported to PHP, Python, and .Net. Whenever you have a framework or library that is being ported to other languages, it shows that the framework being ported contains new and good ideas about programming. In other words, it is a contribution to the paradigm of web development and a clear sign that the original language that the framework was implemented is at the edge of innovation.

Another example is Behavior Driven Development. This example is even more interesting because the idea originally started in Java with the JBehave framework. Even though the idea for behavior driven development started with Java, the idea didn't really take off until it was implemented in RSpec. They are fairly similar in terms of syntax. Here's an example from the JBehave website:

public class CheeseBehaviour extends UsingJMock {
    public void shouldRequireTheUseOfMocks() {
        // given
        Mock cheese = new Mock(Cheese.class);
        Sheep sheep = new Sheep((Cheese)cheese.proxy());

        //expect
        cheese.expects(once()).method("squelch").withAnyArguments();

        // when
        sheep.eatCheese();

        // then
        verify();
    }
}

and here it is converted to RSpec:

describe Sheep do
  before do
    @cheese = mock(Cheese)
    @sheep = mock(Sheep, :cheese => @cheese)
  end
  it "should squelch when it eats cheese" do
    @cheese.should_receive(:squelch)
    @sheep.eat_cheese
  end
end

For whatever reason, JBehave really never took hold in the Java community, but RSpec has in the Ruby community. RSpec has been ported to .Net, PHP and Groovy. All of those projects describe their code as a port of RSpec, not JBehave. Again it is Ruby influencing the wider web application development community.

Post World War II, the center of the art world was New York City and it was there that the modern art movemement was born. New York was where innovation in the art world was happening. In that time period if you wanted to be a serious artist, you had to go to New York to experience the movement first hand. Today, I believe the Ruby community is leading the way in innovative techniques for web application development. There is certainly innovation happening in other languages like Python, Smalltalk and Erlang as well, but I don't think any one other language/community is doing as much as Ruby. As far as I can tell, languages like Java, .Net and PHP are doing nothing to innovate web application development. They are simply lagging behind, playing catch up and trying to figure out how implement new features pioneered in the Ruby community and others as closely as possible, given the limitations of the language. So if you are a web developer, I suggest you ask yourself this question. Are the languages and frameworks you are working with leading others to come up with new ideas? Are the languages and frameworks that you are working helping you come up with new ideas? If not, embrace Ruby and someday you will discover an elegant solution to a problem, one that you may not have without Ruby.

A language that doesn't affect the way you think about programming is not worth knowing. -- Alan Perlis

Posted in  | Tags RSpec, Rails, Python, PHP, Javascript, groovy, Ruby, .Net, HAML, Java | 0 Comments

One Thing About Lisp That Is Better Than Other Languages

3:50 PM EDT Tuesday, March 18 2008

Ruby:

>> 850/550
=> 1

Python:

>>> 850/550
1

JavaScript:

>>> 850/550
1.5454545454545454

Perl:

perl -e 'print 850/550'
1.54545454545455p

Lisp:

[1]> (/ 850 550)
17/11

Posted in  | Tags Ratios, Perl, Python, Javascript, Fractions, Ruby, lisp | 0 Comments

The Web Application Framework Candidates

9:20 PM EST Thursday, February 14 2008

The web application framework race is heating up, so let's take a moment to meet some of the candidates.

Java

George W. Bush

The current leader in the web application framework space, has a declining approval rating from the general public, but still maintains support from members of the static typing party.

Rails

Hillary Clinton

One of the leading candidates from the dynamic typing party. This candidate has experience that proves she can bring change.

Merb

Barack Obama

A candidate from the dynamic party who is quickly gaining support, running on his campaign of hope. Has a stance similar to that of Rails on many of the campaign issues.

Django

John Edwards

Another strong dynamic party candidate, but having a hard time stealing the spotlight from the two dynamic party candidates, despite running on a strong platform.

Grails

John McCain

The leading candidate for the static typing party, particularly among moderate static typers, but having a hard time gaining support from conservative members of the static party who claim that he is too dynamic on some issues.

Scala

Ron Paul

A candidate that appeals to some members of both the dynamic and static typing parties, quickly gaining notoriety on the web for his support of once unconventional ideas like functional programming.

Posted in  | Tags Rails, Python, Django, Ruby, Scala, Java, merb, | 2 Comments

Evolutions - Migrations for Model-Centric Apps

11:20 AM EST Saturday, November 24 2007

There is a project for Django called Evolution, which is conceptually similar to Rails' Migrations. There are some differences between Evolutions and Migrations, mainly due to the fact that Django is model-centric, meaning that you define the attributes of your models in the code and generate the database from that. Rails, or I guess ActiveRecord more specifically, as you probably know, is the opposite, where you define your database tables and an object model is generated at runtime from the database metadata. So I guess you would call that a database-centric ORM. Here's their take on the differences:

Isn't this just ActiveRecord::Migration in Python?

Superficially, yes, but not really.

There is a degree of similarity - the Django Evolution syntax is
a DSL for describing the changes that have been made to a model.

However, there are also some significant differences. In addition to
ActiveRecord::Migration, Django Evolution provides:

    1. An audit trail - a permanent archive in the database of the
       changes that have been applied, and when.
    2. An automated hinting scheme. If you don't want to write the
       migrations yourself, you don't have to.
    3. Validation that an evolution script will leave the database
       in a state consistent with the current model definition.

These differences are largely afforded by the model-centric design of
Django itself. Whereas a Ruby on Rails model is a description of a
database that has been created by hand, Django uses the Python model
to creates the database. As a result, the model definition is canonical
- not the database.

This means that audit, hinting, and verification schemes can use the
Django model as a point of reference. A Django Evolution script is
much more than just an alternate syntax for SQL ALTER statements - it
is an evolution scheme that is bound to the canonical model definition.

The DataMapper project is a model-centric ORM for Ruby apps and as that project continues to grow, their could be some ideas borrowed from Django Evolution, although they do already have their own thing going on with Auto-Migration.

But I'm a Ruby guy, so I've got to take one shot at them here :). They say "the Django Evolution syntax is a DSL". Really?

MUTATIONS = [
     AddField('Author', 'location', models.CharField, max_length=100, null=True)
]

I'm sorry, but if that's a DSL, then what isn't a DSL? In my opinion, there is too much Python syntax to call that a DSL. It's a subtle difference but this is a DSL:

add_column :authors, :location, :limit => 100

The question when is it a language specific library and when it is a DSL? Would you call this a DSL?

Mutation m = new AddField("Author", "location", CharField.class);
m.setMaxLength(100);
m.setAllowNulls(false);
Mutations.add(m);

Posted in  | Tags Rails, Python, Django, data mapper, Ruby | 0 Comments

   Next Page >>