Saturday, February 19, 2011

Ruby On Rails Programming

Lately, i’ve been quite interested in learn more things abour Ruby Programming. To be sincere, i always wanted to learn a bit more about the language, but I’ve been into some WordPress Theme programming the past 2-3 months. Hopefully, i will write some posts about this subject as well, in the future.

What is Ruby on Rails ?

Ruby, as you may know, is a very well known scripting language, with an exceptional and clean syntax. I’ve always been a fan of Python and i must say that moving to Ruby is a totally different programming style. Luckily, learning Ruby is quite easy. It indeed offers some very good techniques, like code blocks etc, but overall, it resembles interpreted language, like Python and Perl also are.

On the other side, Ruby on Rails is something completely different and harder to learn. RoR (widely used for Ruby on Rails), is actually a framework for creating web applications. As simple as that. You may already know the Django alternative for Python. Sounds … ok, but is it just that ?

Why Ruby on Rails is Really Really Good

RoR is really great because it makes the life of the programmer a WHOLE lot easier. Imagine that you wanted to write a php function that checks whether  passwords match in a simple registration routine. Using php, you would need to get the input, compare it and write an html page that presents the error to the user. Lots of work for doing a simple thing. But with ruby :
view source
print?
validates_confirmation_of :password

is just enough !

Installation of RoR

Installing RoR is a pretty easy procedure for the well known operating systems. There is an installer for Windows, you can use a package manager for Linux and macports for mac OS (as i currently use). For more information on the whole process, please take a look at the official ruby programming website.

The MVC (Model - View - Controller) Model.

MVC is the programming model that RoR utilizes. If you have used a templating system like smarty in the past, you have most probably already used the model. This model simply identifies that our web program has 3 different entities that we need to look after. The model, the view and the controller. But, what is the difference ? Now, i could describe that in words, but i prefer providing an example as well :

The View is the easy entity. It is what our program prints out to the browser of a user.

The Controller closely relates to data. It is the part where data is initialized, to be then provided to the View and shown to the user.

The Model is the auxiliary entity where we write our large functions that operate on data.

Now, think of this situation. You have a page that presents some products for people to buy. The Controller is the first thing that gets utilized. Once the browser requests the page, the controller takes action. Therefore, when the products page is requested, the Controller understands that this page was called and provides the gateway between your large functions and your view. Now, the Model contains functions such as getAllProducts(). This function is called by the controller, to provide an instance of all the products. This variable, most times a class variable like @products, is going to be used in the view.

Hope this starts to make sense, i've tried to describe it to the best of my knowledge. I do not want to take this post any further. In my next one about RoR, i will describe how to create a test project, use migrations and more. Have fun with Ruby, play a little bit with it, it is really worth it, you have my word !

Ruby on Rails

There’s been no shortage of “magic bullet” technologies over the years, and one of the hottest things in web development at the moment is Ruby on Rails.   At Headshift we’re using Rails for an increasing number of projects, and we’re seeing more and more interest from clients in this technology.   Having said that, it’s not always clear what Ruby on Rails actually IS, so what follows is an overview of the whats and whys, in as non-technical terms as possible.

Java, dot Net and even historical aberrations such as Visual Basic have all been up and down the hype curve over the years, so you could be forgiven for regarding the interest in Ruby on Rails as more of the same.   As a consultancy we pride ourselves on being technology-agnostic – but nevertheless, we’re Ruby on Rails fans for a number of reasons.

What is Ruby on Rails?

Ruby on Rails is – depending on who you’re talking to, and the context in which you’re talking to them in – a number of different things:

It’s a set of technologies

Ruby on Rails, as the name suggests, is two things:  Ruby is an object-oriented scripting language which has been around since the mid-1990s, and has developed a devoted following from developers who appreciate its simplicity, readability and flexibilty.

Rails is a framework written in Ruby, and it’s the nature of the framework that makes RoR so powerful.   An awful lot of what developers do is covering old ground – most web applications have a database behind the scenes, for example – so most web applications will need database integration.   One approach is to rewrite this from scratch every time – while it’s an approach, it’s one that can be slow, expensive and prone to bugs.

A framework on the other hand takes common design patterns such as database handling and provides a set of standardised functionality that new applications can hook into.   This makes it a lot quicker to set up these standard functions – and also makes it less likely that bugs will be introduced, because if lots of applications are using the same functionality it’s likely that bugs will be quickly spotted and squashed.

It’s a set of design approaches

Rails is an “opinionated” framework – it has a set of ways of doing things, and while these can be overridden when necessary, in most situations it’s easier and quicker to use the standard approach.  This can be summarised as “convention over configuration” – for example, Rails has a standard way of abstracting an underlying database structure into classes, objects and attributes which means that it will behave in the same way from application to application and developer to developer.

Rails also uses the “model, view, controller” design pattern (or MVC for short).   This is something that’s been around successfully for years – it breaks down an application into the underlying data and business logic (the model), presentation of data to users (the views), and the code that orchestrates the interactions between model and views (the controllers).  There are some subtle but powerful advantages to an MVC approach – for example, if the models are responsible for maintaining the database and enforcing business logic, if a field must have a certain number of characters or value this can be enforced in one place (the model) rather than relying on checking at every point where the values might be changed.

“Don’t repeat yourself” is another core Rails “philosophy” – if an element of a page such as a menu is used repeatedly, then rather than rebuilding it time and time again, Rails makes it supremely easy to abstract it into a module which can be reused.

It’s a community

Both Ruby and Rails are open-source, and there’s a vibrant community which has grown up around them.  The chances are that someone has experienced – and solved – your problem at some point in the past, so rather than having to reinvent the wheel the Rails community is valuable source of help and advice.  That’s not to say that other technologies don’t have communities, but Rails seems to attract people who are naturally at home with open-source ways of working.  This also helps to explain the rapid growth in the number of plugins and code modules that have been written to extend the framework.

Why is Ruby on Rails so hot?

This is a topic that’s worthy of an entire blog post in its own right, so I’ll just look at a couple of reasons.   Rails development fits very well with Agile project management techniques – which can result in true rapid application development.  Clients are often surprised at how quickly a Rails application can be put together, and how flexibly it can respond to changes in requirements as they are discovered over the course of the project.

Speed doesn’t necessarily have to sacrifice quality, though – testing is crucially important for creating reliable software.   Rails treats testing as an integral part of development rather than an afterthought – to the extent that it’s almost easier to test than not!   A particularly powerful technique is the use of “continuous integration”.   As development takes place, tests are developed along the way – and when a new piece of code is written, all the existing tests are run to ensure that the new functionality doesn’t inadvertently break something elsewhere.

And I’ve mentioned the fact that Ruby and Rails are open-source – again the topic for a whole blog post, but the combination of quality resulting from “many eyes” looking at the code and the low cost make open source an attractive proposition in the current economic climate.