Ruby the programming language created by Yukihiro 'Matz' Matsumoto has recently come into the limelight and is slowly gaining traction with the programming world. This can be, in part, attributed to RubyonRails, a web development framework, written entirely in Ruby. Before we explore more on the framework, let's dwell on the language.
Ruby is essentially a scripting language. Talking about some of its important features, it's thoroughly object-oriented, dynamically typed and an imperative programming language. It is, in many ways, similar to Python, but differs from it in case of execution and syntax (Introduction to Python here. What sets Ruby apart from the pack, is the fact that it is very easy for beginners to grasp, and at the same time powerful enough for the most advanced programmers.
One of the best uses of Ruby is as a prototyping language, you can have fully-functional working model of your application, in the same time you may have spent floundering to set up the framework while using other programming languages. But its application is not limited to prototyping. Ruby is used extensively almost across the entire spectrum – a programming language can have simulations, 3D modelling, business, robotics, telecommunication, web applications etc.
We'll discuss the current stable iteration of the language i.e. Ruby 1.9.2. There are three options you can follow to install Ruby:
- Compile from the source code
- Use RVM (Ruby Version Manager)
- Use Ruby Installer
To compile Ruby from source code, head over to the Download Ruby page on its website and grab the latest source-code tarball, and compile it for your system. Mindwell, this will only work for *nix-based systems and not for Windows. For *nix-based operating systems a better option (and a recommended one too) would be to use RVM which stands for "Ruby Version Manager", which allows you to install and manage multiple copies of Ruby on your system, as well as multiple alternate implementations of Ruby. Installing RVM is slightly more aggravating, you will need to follow the instructions for a 'Single User Install' as mentioned here. Make sure you have curl and other dependencies resolved (these dependencies are same as the ones encountered when compiling any software from its source code).
NOTE: If you are running Linux, your distribution repository may also have Ruby e.g. For Ubuntu use the command sudo apt-get install ruby1.9.1 (This will install 1.9.2, the name is kept for library compatibility) On other systems it is prudent to use RVM. Mac OS X has a built-in support for Ruby (one of the reasons, for Ruby becoming famous) Lion supports 1.8.7, again here RVM is advised as Mac OS X is a Unix based system.
If you are running Windows, neither of the two methods mentioned above will work for you. Download the Ruby installer from its website. Run the setup and you will be greeted with the usual fare; license and so on. The important part in the setup is selecting which features you want, when you reach the screen, same as the one shown below, select options as shown (We've left out the installation of Tcl/Tl toolkit as we will not be working on GUI-based programs, you can install it you do plan to work on the same)
The customary greeting
It is an unwritten rule in the world of programming that, you cannot start learning any programming language without greeting the world, first-hand. So we'll start with simple Hello World! Program. Fire up the terminal or the command prompt, type in irb (which stands for Interactive Ruby, and is a fantastic way to learn and test code in Ruby) and hit enter, you will be greeted with the irb prompt, enter the code snippet shown below puts “Hello World!!”
Yes, it may seem unbelievable but you just ran your first Ruby code. If this is not tough or challenging enough for you, we’ll execute the same program in a different way. Open Notepad or any text editor of your choice, enter the same code snippet and Save the File as hello.rb Now fire up the command prompt, navigate to the folder where your source code recides and enter the command below
The above program greets the world 5 times, and the syntax is also much simple and more intuitive as compared to other languages, the same program in C would look something like this
This is one of the many advantages of using Ruby; the syntax is so simple, it seems that you are giving commands to the interpreter in English. The program can be read: 5 time print “Hello World!!” on a new line and hence it is very easy for novices to grab the intricacies of the language
NOTE: The example discussed, justifies the statement that Ruby is thoroughly or we can say strictly object-oriented. Even a primitive data type such as Integer is encapsulated in an object. (For those not familiar with object-oriented programming an introduction to OOP here)
Executing your Ruby Scripts
Moving further, those of you who have a little background in programming will be able to recognize this pattern
Now how would you have gone about writing a program which generates the aforementioned pattern in C? The code would most probably look something like this:
Now coding the same program in Ruby will look like this:
Here we’re using until as a modifier, which causes a statement to be executed repeatedly until the condition is met, the syntax in its simplest form looks like:
Expression until Condition
Also the variables are not declared, you can just assign value to variable to use it. The variables also do not need to be type-casted, Ruby handles that for us. And we’ve used print here instead of puts, as puts adds a new-line character at the end of the string, which will result in a line rather than a triangle of made up of *s. What changes, do you think, will have to be made to get a pattern like this:
Keep in mind, that the code mentioned here can be directly executed using irb or you can go for the other method in which you make .rb file and run it using the Ruby interpreter (i.e. the ruby command on the command-line or terminal).
Other Popular Deals
- Top launches of the week: May 22, 20156 weird inventions that tried too hard
- Top launches of the week: June 5, 2015Top launches of the week: May 29, 2015
- Top stories of the week: May 22, 2015Top launches of the week: June 12, 2015
- Top stories of the week: May 29, 2015The Intel Compute Stick, in pictures
- Top stories of the week : June 12, 2015Top stories of the week: June 5, 2015
- In pictures: ETI Dynamic's Solar Electric Hybrid Vehicle17 upcoming movies of 2015 that have us excited
- 5 great gadget deals under Rs 10,000Top stories of the week: May 15, 2015
- Best tech you can buy on a budgetTop launches of the week: May 15, 2015