Thanks for visiting my website. Today we will understand Ruby on Rails (RoR). Ruby is an object oriented programming language. First time when I heard about Ruby, I also heard that it is scripting language. So first thought which came in my mind was scripting….seems like some toy language. But to remind you Ruby is not a toy language which I realized after learning more on Ruby. Roby on Rails (RoR) is fully MVC based web framework for developing database driven application.
Little on history, Ruby was developed by Japanese scientist Yukihiro Matsumoto(Matz) developed Ruby in 1993. I had quite difficulty in pronouncing his name but I settled with Matz which is much easier. He wanted to develop a programming language which is more powerful than Perl and more object oriented than Python. People who started programming much later including me learnt about Python as part of their graduation course. RoR was developed by Danish programmer David Heinemeier Hansson. David was developing framework for Basecamp project. He wanted to have a rapid development framework without compromising on good design and best practices. Once he found framework very good for developing Web based application for Ruby based programming language, he released it to outside world in 2004 without commit right. Later with commit right, it was released in 2005.
Now the big question, why should we really care about learning RoR. Before jumping in to understanding the reason to learn RoR. Let us understand the current context of software development. Let us go little backward where we used to have great waterfall model. Imagine one customer had an idea and wanted to develop software around that idea. He/She hired our company who is expert in software development to build software around his idea. So being expert in software development we suggested Water Fall model of development. We will start with fully documented requirements(to make sure there is no conflicts during user acceptance) followed by Architecture, Detail Design, then development, testing, lot of bug fixing (including change request coming from client), User Acceptance Testing and then delivery to customer. This entire exercise used to take minimum of 4 – 6 months. So we delivered software as agreed by requirements in 4 – 6 months. Let’s think from customer perspective to know what happened to him. He/She had idea around six month back and took around 4 – 6 months to develop that idea into software to test into market whether his software idea would work or not. Once he/she launched his software into market, lot of market condition have changed and delivered software was not fitting into the need of market and software failed. Whom to blame? Customer who didn’t have great idea, his model of testing idea into market, Software Development Process, Tools used for developing Software, many others. May be all of them. But for customer he has failed and lost his money. There is nice book around Lean Startup by Eric Ries, who tells us how to build on top of your ideas with constant feedback loop with starting very small. You can have look on http://theleanstartup.com/ to understand the detail on that. As software company did we help him/her being successful in his/her venture? May be not. But how can we help customer in their success. Definitely by suggesting process and tools which can help him/her going quicker in market to get the feedback loop ready earliest. As process we can suggest definitely Agile but tools side we need to suggest tools which supports Rapid Development without compromising on good design and best practices. RoR is one of these kind of tools which has gained reputation in recent times.
According to RoR community you can develop 10x faster in RoR than .Net. Should you believe it? May not be but definitely it is much faster than general .Net development. What I have seen is definitely more than 5x. One of the key strength of RoR is Ruby language which has best features of dynamic language, static language and all blended in OPPs. There is lot of good community around Ruby from where you can get great help.
I was still not convinced if this is enough reason to learn RoR and tried to find out current growth rate in jobs which being created and tried to compare with other technologies like .Net, Java and PHP. Below is the stat which I got from Indeed.com.
I argued myself as the number of RoR development is less that could be reason why growth rate is high. But even then if there is a technology which is growing at such a good speed, we may not ignore it. There are good number of famous sites which are on this technology like GitHub, Basecamp, Groupon, YellowPages, Shopify, Twitter(till last year), etc. So industry is finding it as trending technology, so we may need to look at it. Then the last argument which I gave to myself as people are becoming polyglot, it is good that I would be exploring another technology. I am not going to give you steps to download and install as people are great in googling things.
Let’s see some of the key features of Ruby programming language.
- Interpreted
- Fully Object Oriented
- Duck Typed
- Enforced Naming Convention
- Classes are open
- Supports Keyword Argument
- …….
In interest of time let me explain you some of the above which looked interesting to me as a .Net developer.
- Fully Object Oriented – In Ruby everything is object. It is more hardcore object oriented programming language. Every element of the code is Object. Just to explain you if you look at below console which is irb(Console for interactive Ruby development), “4” literal is also a class. So you can type “4.class” in console and it would return you “Fixnum”. If you type “4.methods”, it would give you methods for that class. I would leave rest for you to explore.
- Duck Typing – The statement goes like this “If an object walks like a duck and talks like a duck then the Ruby interpreter is happy to treat it as if it were a duck”. Interesting as coming from statically type language, you might feel this surprising. We have learned entire polymorphism concept around this. To understand how is it different in Static Typing and Dynamic Typing, let us understand what happens in case of static typing and what happens in case of dynamic typing. Imagine we want to create some kind of family of Duck class which supports Quack functionality.
Static Typing
So to implement in static typed language we need to create a base class “Bird” which will support “Quack”. “Quack” function would be virtual, so derived classes can override it. Then we need to derive “Duck” class which will override “Quack” functionality to have it’s own implementation. Now we need to create variable of “Bird” type.
This variable “variableBIrd” can point to all types which are derived from “Bird”. So we can create instance of any “Duck” type and set to “variableBird” but this will fail when we try to set instance of another class “Goose” which supports “Quack” functionality.
The option we have now is to derive “Goose” from “Bird” and then only “variableBird” can point to instance of “Goose” type. So in “Goose” we also need to override “Quack” function, so we can call it base class level and would be overridden call to “Goose” class.
Dynamic Typing
Enter the world of dynamic languages. You are not required to do this exercise in case of dynamic typing. As long as Goose supports a function which has same signature as it is in case of Duck, variableBird can point to Goose.
You can understand this from the below code which is written in Ruby.
Dynamic languages comes with their own cost but we would not go in that discussion/argument. Just understand this as feature. Even C# has also come with dynamic feature. If you have still not explored it you may be interested in exploring sometime in future.
Now let us see some of the key features of RoR(Ruby on Rails).
- MVC based framework.
- Uses lot of scaffolding for code generation.
- Active Record based ORM.
- Full Testing Support.
- DB Script as part of development
- ……..
In interest of time, let me explain some of the key features which would interest you.
MVC based framework
In any MVC based web framework, you would have above component. It starts with client sending request to server. In this case client is Web browser sitting on client machine which sends HTTP request to Web Server. Now the responsibility of the server is to process the HTTP request and send response to client. Now it is the responsibility of web server to provide option to programming platforms(ASP.Net, PHP, RoR, …) to handle that request and do something extra . You have generally component setting on Server to handle this request in different ways depending on programming platform. In programming platform component(s) required for implementing MVC based web solution, we have Dispatcher, Controller, Model, and View. Dispatcher has routing logic to select specific controller, instantiate it and then call Action method on it. Now Controller adds/gets/updates/delete model data based on kind of request sent to server. Once this action is performed, it selects what view should be returned to client. That selected view is returned to Web server which finally packs that View in HTTP response and sends it to client. This is how Web based MVC based framework works. How to implement business logic, persistence logic(storing data), calling different services, etc is Architectural decision which you make depending on functional and nonfunctional requirement. RoR complies to MVC in standard way.
Active Record based ORM
In above diagram I didn’t explain Model Active Record and left it for this section. There are various ways to implement persistence logic. Active Record is one of the pattern suggested by Martin Fowler to implement ORM mapping. According to this pattern, “Active Record is an object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data”. This pattern suggests to add persistence (Data Access) logic in model itself.
Imagine we want to implement Active Record for one Employee table which has “Name” and “Address” and two fields. You will create one class “Employee” which would have two properties “Name” and “Address”. You would set specific property depending on type of fields in database. Let’s imagine we would be supporting only two option for persistence. So in this case, we would need two functions called “Insert” and “Update” for this class. Now in memory when we would be using this class, there would be as many as instances as records in database. So here in this case we would expecting five instances of “Employee” class. For brevity I have shown only two instances here. RoR uses Active record for persisting the data.
Lot of scaffolding
I would leave others for you to explore. Something which is worth mentioning here is scaffolding features. In RoR you can create entire working site in 30 min using scaffolding features. To start with you can download RoR from their official site. You can create new application using “rails new <ApplicationName>” on command prompt which has access to rails command(most probably in the installed directory of Ruby). This would create entire project structure for you.
I am excluding other instructions for brevity and repetition of something which is well document on RoR web site. You can follow instruction on getting started section on RoR website to create one simple blog application. Believe me it should not take you more than 2 hours to create one working demo website with good design and best practices in RoR. I leave you here so you can explore other aspects of RoR.
So to conclude, RoR is MVC based web framework on Ruby which has really nice features. If you have still not got chance to explore it, it is worth exploring. I just saw one of my friends posting job opening of RoR on Facebook recently. I will leave it for you to find it :). Thanks again for visiting my web site. Happy learning.












