Ruby on Rails 2.1 - Introduction



What is Ruby?

Before we ride on Rails, let us recapitulate a few points of Ruby, which is the base of Rails.

Ruby is the successful combination of −

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

Ruby is

  • A high-level programming language.
  • Interpreted like Perl, Python, Tcl/TK.
  • Object-oriented like Smalltalk, Eiffel, Ada, Java.

Why Ruby?

Ruby originated in Japan and now it is gaining popularity in US and Europe as well. The following factors contribute towards its popularity −

  • Easy to learn
  • Open source (very liberal license)
  • Rich libraries
  • Very easy to extend
  • Truly object-oriented
  • Less coding with fewer bugs
  • Helpful community

Although we have many reasons to use Ruby, there are a few drawbacks as well that you may have to consider before implementing Ruby −

  • Performance Issues − Although it rivals Perl and Python, it is still an interpreted language and we cannot compare it with high-level programming languages like C or C++.

  • Threading model − Ruby does not use native threads. Ruby threads are simulated in the VM rather than running as native OS threads.

Sample Ruby Code

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
   end

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

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

# Call ruby method
obj.salute

This will produce the following result −

Hello Ruby

For a complete understanding on Ruby, please go through our Ruby Tutorial

Embedded Ruby

Ruby provides 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 encounters a Ruby code, it starts 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 a part of the output, enclose the code between <%= and %>.

Here's an example. Save the code in erbdemo.erb file. Note that a Ruby file will have an extension .rb, while an Embeded Ruby file will have an extension .erb.

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

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

c:\ruby\>erb erbdemo.erb

This will produce the following result −

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

What is Rails?

  • An extremely productive web-application framework.

  • You could develop a web application at least ten times faster with Rails, than you could with a typical Java framework.

  • An open source Ruby framework for developing database-backed web applications.

  • Your code and database schema are the configuration!

  • No compilation phase required.

Full Stack Framework

  • Includes everything needed to create a database-driven web application using the Model-View-Controller (MVC) pattern.

  • Being a full-stack framework means all the layers are built to work seamlessly with less code.

  • Requires fewer lines of code than other frameworks.

Convention over Configuration

  • Rails shuns configuration files in favor of conventions, reflection, and dynamic run-time extensions.

  • Your application code and your running database already contain everything that Rails needs to know!

Don't Repeat Yourself (DRY)

DRY is a slogan you will hear frequently associated with Ruby on Rails, which means you need to code the behavior only once and you never have to write similar code in two different places. This is important because you are less likely to make mistakes by modifying your code at one place only.

Rails Strengths

Rails is packed with features that make you more productive, with many of the following features building on one other.

Metaprogramming − Other frameworks use extensive code generation from scratch. Metaprogramming techniques use programs to write programs. Ruby is one of the best languages for metaprogramming, and Rails uses this capability well. Rails also uses code generation but relies much more on metaprogramming for the heavy lifting.

Active Record − Rails introduces the Active Record framework, which saves objects to the database. The Rails version of the Active Record discovers the columns in a database schema and automatically attaches them to your domain objects using metaprogramming.

Convention over configuration − Most web development frameworks for .NET or Java force you to write pages of configuration code. If you follow the suggested naming conventions, Rails doesn't need much configuration.

Scaffolding − You often create temporary code in the early stages of development to help get an application up quickly and see how major components work together. Rails automatically creates much of the scaffolding you'll need.

Ajax at the core − Ajax is the technology that has become a standard to provide interactivity to websites without becoming intrusive. Ruby on Rails has a great support for Ajax technology and it is a part of the core libraries. So, when you install RoR, Ajax support is also made available to you.

Built-in testing − Rails creates simple automated tests you can then extend. Rails also provides supporting code called harnesses and fixtures that make test cases easier to write and run. Ruby can then execute all your automated tests with the rake utility.

Three environments − Rails gives you three default environments - development, testing, and production. Each behaves slightly differently, making your entire software development cycle easier. For example, Rails creates a fresh copy of the Test database for each test run.

What is Rails 2.1.0?

This is the latest version of Ruby on Rails, which has been released by the Rails core team on Saturday May 31, 2008.

This version is a further improvement on RoR 2.0, which was again really a fantastic release, absolutely stuffed with great new features, loads of fixes, and an incredible amount of polish over its previous versions RoR 1.2.x.

This tutorial takes you through all the important features available in the latest RoR version 2.1.0.

After this tutorial, you should be able to build your website using one of the best Web 2.0 technologies called Ruby on Rails v2.1.0.

Advertisements