Ruby on Rails 2.1 - Preliminary


Before we ride on Rails, let's understand a little about Ruby Programming Language which is the base of Rails.

What is Ruby?

Ruby is a interpreted programming language developed by Yukihiro Matsumoto in 1995 and today Ruby is the fastest growing language.

Ruby is the successful combination of −

  • Smalltalk's conceptual elegance
  • Python's ease of use and learning
  • Perl's pragmatism

Ruby is −

  • A High Level Programming Language
  • Interpreted like Perl, Python, Tcl/TK.
  • Object-Oriented Like Smalltalk, Eiffel, Ada, Java.
  • Originated in Japan and Rapidly Gaining Mindshare in US and Europe.

Why Ruby ?

Ruby is becoming popular exponentially in Japan and now in US and Europe as well. According to the TIOBE Programming Community Index, it is the fastest growing language.

Following are greatest factors which are making this language the fastest growing one among the other languages −

  • Very easy to learn
  • Open source (very liberal license)
  • Rich built-in libraries
  • Very easy to extend
  • Purely Object-Oriented
  • Less coding with fewer bugs
  • Automatic memory management
  • Duck Typing, which means Dynamic Typing
  • Helpful community

Why Not Ruby ?

  • Performance − Although it rivals Perl and Python but because of being interpreting language, we can not compare it with high programming lanaguage like C or C++.
  • Threading Model − Ruby does not use native threads. This means Ruby threads are simulated in the VM rather than running as native OS threads.

Ruby Syntax −

Here is a sample Ruby code to print "Hello Ruby"

#!/usr/bin/ruby -w

# The Hello Class
class Hello
   # Define constructor for the class
   def initialize( name )
      @name = name.capitalize

   # Define a ruby method
   def salute
      puts "Hello #{@name}!"

# Create a new object for Hello class
obj ="Ruby")

# Call ruby method

This will produce following result −

Hello Ruby

Embeded Ruby

Ruby provides you with a program called ERb (Embedded Ruby), written by Seki Masatoshi. ERb allows you to put Ruby code inside an HTML file. ERb reads along, word for word, and then at a certain point when it sees the Ruby code embedded in the document it sees that it has to fill in a blank, which it does by executing the Ruby code.

You need to know only two things to prepare an ERb document −

  • If you want some Ruby code executed, enclose it between <% and %>

  • If you want the result of the code execution to be printed out, as part of the output, enclose the code between <%= and %>.

Here's an example, Save the code in erbdemo.erb file. Please note that a ruby file will have extension .rb but Embeded Ruby file can have extension as .erb.

<% page_title = "Demonstration of ERb" %>
<% salutation = "Dear programmer," %>
      <title><%= page_title %></title>
      <p><%= salutation %></p>
      <p>This is an example of how ERb fills out a template.</p>

Now, run the program using the command-line utility erb.

c:\ruby\>erb erbdemo.erb

This will produce the following result−

      <title>Demonstration of ERb</title>
      <p>Dear programmer,</p>
      <p>This is an example  of how ERb fills out a template.</p>