Ruby on Rails – An introduction

Updated on 06-Nov-2014
HIGHLIGHTS

One of the most popular languages currently is quite easy to get into after all it puts the user ahead of everything else

Before we get into understanding what Ruby on Rails is we need to know a bit about Ruby, the user friendly language that has taken the world by storm.

A little about Ruby

Ruby is a programming language created by Yukihiro Matsumoto in the 90’s. The first version was officially released in 1999. It’s similar to C and Java as they are all general purpose programming languages. Moreover, it is one of the most popular programming languages out there and most of it is because of the popularity of Rails.

So what’s this Rails thingy?

Simply put, Rails is a software library. It extends Ruby and adds a lot more functions allowing for rapid development. It was created by David Heinemeier Hansson. In Ruby, each package, or library that extends the language is called a Gem. Rails is one such RubyGem. Rails enables conventions for allowing collaboration when building websites. All of these conventions are put together as the Rails API which has been documented extensively and has immense popularity online. With a huge community behind you can be assured that it’ll be around for quite a long time. A significant portion of the community is made up of startups. This community comes together to improve Rails collaborating on standards so that complex sites can be developed rapidly.

Going about using Rails

Starting off with Rails is more about learning the conventions noted in its API since that is all there is to it. Rails brings together HTML, CSS and Javascript to create web applications that run on a web server. And as such due to the requirement of a web server, this language is considered to be server-sided or back end application development platform.

Basic concepts

With all programming languages, there is an underlying philosophy which guides the growth and popularity of the said language. Rails is no different.

1. Opinionated

With programming languages there are often multiple ways of going about programming an application. A non-opinionated language is one that doesn’t direct you to program in a certain fashion. Take Perl as an example, there is no right way or wrong way of going about getting to your objective. So there will be approaches that may not be as efficient as others. Rails on the other hand is opinionated. There is a right way about programming in Rails and this helps reduce the number of decisions one has to make along the way. Also, collaboration is made easier since everyone is following the same style of programming.

2. Convention over Configuration

This is simply why Ruby is described as opinionated. There are certain conventions that are followed strictly in Rails. In other languages if you were to create any method or function and describe all the variables, you’d have to specify each and every one of their data types and lengths. Ruby isn’t completely devoid of this flexibility but it does make a few assumptions. If you were to create a model object in Rails and call it “User”, then Rails will automatically assume it to be plural and save a database called “users” without the need for any extra configuration needed. This allows the programmer to spend more time on the main concept of an application rather than worry about the nitty gritty details which might throw a spanner in the gears.

3. Don’t repeat yourself

Avoid duplicating your code. Not only does duplication make code more complex but it also makes it difficult to maintain and provides avenues for more bugs to creep into the code. This is not only limited to how your program but also to the development process as a whole. An example would be to use automated testing methodologies instead of manually testing each and every facet. DRY (Don’t Repeat Yourself) is a really old philosophy is all about reusing code over and over again. Rails makes excellent use of the metaprogramming feature of Ruby to not only reuse code but also eliminated unwanted lines of code as and when possible. This makes for a really lean code for an application provided you know most of the API.

The first page you see when you test out a new web application

So, who all use Ruby on Rails?

We’ve mentioned earlier that Rails is popular among startups. But bigger companies aren’t exactly shying away from it. Here are some of the most popular websites that use Ruby on Rails.

1. Twitter

Everyone’s favourite 140-character social website is largely built using Ruby on Rails

2. Shopify

This service which allows you to build your own web store with minimal effort uses RoR to a great extent

3. Crunchbase

A database which is all about startups, including companies, people and investors.

4. Groupon

Coupons for everything! Caters to a plethora of stores across many countries

5. Bloomberg

Most of their web applications are coded using Ruby on Rails

Starting off with Rails

1. Getting it all together

You’ll need to download the latest release of Ruby. It comes with RubyGems packaging system. Also an installation of SQLite3 Database is needed since this is a server-sided language. Windows users can use RubyInstaller while Linux and Mac users can go with rbenv or RVM to install Ruby on their machines. Or on all platforms you can compile the code if that’s how you roll.

2. Install Rails

Once Ruby is installed you need to install Rails. Simply open up the command prompt or Terminal app if you’re using Mac OS. Check if the system path is proper by querying the ruby version by typing “ruby -v” without the quotes. The prompt should display your current installed version number, if not then add the path to the binaries in the system variables tab.

Now if Ruby is installed properly, move onto SQLite3. If you’ve downloaded and installed it properly then it too should appear in the path. Query the version to make sure it’s installed properly by typing “sqlite3 –version” without the quotes in the command prompt.

If everything worked out fine then installing Rails is as simply accomplished by making use of the install command provided by RubyGems. The syntax is “gem install rails”. Once it’s done with query the version yet again using the command “rails –version”. If you get a proper answer with the installed version number then you are ready to start developing in Rails.

Blue – Ruby, Red – Javascript & Yellow – PHP trends over the years

3. Creating your first application

Rails has a lot of scripts called Generators which can be used to create the entire folder structure and download all the dependencies along with it. If you wish to create a blog application in rails then type in “rails new blog” to create a blog application. This is a fully functional application which you can access by getting into the appropriate folder. In this case it will be “blog”.

4. Start a web server

Since Rails is a server-sided language you need a web server running on your machine to actually run your application. Rails has its web server called WEBrick that can be run using the command “rails server”. Once the web server is up you can open any browser window and navigate to http://localhost:3000 to view your application. You should see a Welcome Aboard page which makes sure that everything needed (Ruby, Rails and SQLite3) is installed properly and is functional.

Model-view-controller

Rails follows the MVC pattern which is about breaking the application down into three distinct segments.

Models are the Ruby classes that talk to the database and perform business logic and does most of the work.

Views are what the users get to see. This means, HTML, CSS, XML, Javascript etc are all part of Views.

Controllers do the work of communicating between the user and the application like managing cookies, user requests and submissions.

Following this basic pattern allows easy collaboration since everyone who is working on the application knows roughly where each portion of the code is stored.

Ruby follows the Model-View-Controller pattern

 

Resources

Learning Rails is all about getting to know the different conventions that are followed. To make the job easier (since it is quite simple to get started with), we’ve curated a list of some popular online tutorials that are free and also quite lucid.

1. Introduction to Programming with Ruby

http://www.gotealeaf.com/books/ruby

2. Why’s (Poignant) Guide to Ruby

http://mislav.uniqpath.com/poignant-guide/

3. Learn to Program

http://pine.fm/LearnToProgram/

4. Learn Ruby the Hard Way

Mithun Mohandas

Mithun Mohandas is an Indian technology journalist with 10 years of experience covering consumer technology. He is currently employed at Digit in the capacity of a Managing Editor. Mithun has a background in Computer Engineering and was an active member of the IEEE during his college days. He has a penchant for digging deep into unravelling what makes a device tick. If there's a transistor in it, Mithun's probably going to rip it apart till he finds it. At Digit, he covers processors, graphics cards, storage media, displays and networking devices aside from anything developer related. As an avid PC gamer, he prefers RTS and FPS titles, and can be quite competitive in a race to the finish line. He only gets consoles for the exclusives. He can be seen playing Valorant, World of Tanks, HITMAN and the occasional Age of Empires or being the voice behind hundreds of Digit videos.

Connect On :