Ruby on what?

Techie and I as well as a few friends went to Bar Camp a while back now. I think being a little pessimistic we slightly dismissed the whole thing due to the lack of business awareness it seemed. Now having time to reflect the one thing that I did enjoy was the events ability to increase awareness of new trends. It was really hard not to notice that every 2nd person seemed to have some interest in Ruby on Rails. After looking into this further I have decided to explore my findings.

What is Ruby on Rails?

Basically Ruby is a programming language similar to many we have today; Ada, Python, Perl, Php (did I just mention ada). It originated from Japan and dates back to the 1990s. However it’s popularity has increased over the last few years due to much more English documentation being made available. What makes Ruby different from other programming languages is that its focus is on simplifying much of the mundane repitition with a strong focus on productivity.

Ruby was built from the ground up having major influence by a language called Smalltalk. The idea was to consider everything as an object. Many of the features of Ruby have been designed to make it easier for the programmer with the main issues being described here. However, one of the features that stood out for me was Ruby’s ability to offer multithreading regardless of which platform Ruby runs on. In one of my previous posts I talk about using Ajax to give the illusion of multithreading (concurrent xml), its still not true concurrency.

Rails

Rails is an open source framework using Ruby to develop web and database driven applications similar to what you would expect from Php on Apache and the dotnet framework. Once again with the focus being on simplification rails allows you to write standard web applications up to 10 times faster. For example, every Rails project starts the same way by generating a wack of standard code that normally you would have to create yourself, such as test suites, image folders, db config files, robots file and a set of standard scripts that you will inevitably use. So this already has saved you a good hour.

Frequently with database driven applications we need to be able to read, write, update and delete the data from our database. Regularly I have to create admin functions that do just this for a given set of tables. This is somewhere that Rails really comes into play. Using built in functionality, once the DB tables are created Rails can automatically generate the code to manipulate the data from these tables. To do this in Rails you use something called scaffolding :

ruby script\generate scaffold customers customers 

This basically tells Rails to generate the Model, View and Controller for editing the customers tables. Looking at the code it generates you can see which parts relate to the update, create and deletion functions e.g. create function

def create
     @customer = Customer.new(params[:customer])
     @customer.date = Time.now

     if @customer.save
          flash[:notice] = ‘Customer details saved successfully’
          redirect_to :action => ‘list’
     else
          render :action => ‘new’
     end
end

One of the strong points of the language is the way it communicates with the DB for simple statements. No need to type ”INSERT INTO customers VALUES…” or  ”SELECT * FROM customers”, but quite simply :

Customer.find(params[:id])
Customer.new(params[:customer])

Obviously if you would like to add some custom business logic in, that can be done, but you have saved yourself time by having that created automatically. There is nothing worse than creating edit,update,delete pages and this is a great bonus.

The downside to all the automatically generated code is that for something I would consolidate into 1 file, Rails generates about 15, with 1 for each function for the code, and for the view. While it’s not really a problem, for those of you who like a really clean tidy directory structure, it will take a bit of getting used to.

Learning

Like many this is my first look at Ruby on Rails and I can certainly see from a developers point of view it has many advantages, specifically with reference to the code generation, almost the logical step forward from code completion in a sense. However even as a developer I am slightly wary of all the hype, yes it is simple, but I would imagine it will still take time to get your head round this new model of programming. Considering most web projects dont follow the tradition model-view-controller rules it could take some time for beginners to understand this too. Another question that puzzles me, a lot of the commands are run directly from the prompt. This can’t leave much scope for companies without direct access to the server, although I do imagine some sort of admin provision will be made for this by ISPs that support it.

Still, I like it and will certainly invest some time into learning this new language with the goal of using it for a project in the future. I’ll keep you updated.

 

2 Comments »

  1. Ramon Leon said,

    March 26, 2007 @ 3:27 pm

    Minor correction, it’s Smalltalk, not SmallTalk. Small common error in an otherwise good article.

  2. HWHappy said,

    March 26, 2007 @ 3:29 pm

    Thanks for that Ramon, I’ve corrected that now.

RSS feed for comments on this post · TrackBack URI

Leave a Comment