Copyright © tutorialspoint.com

Ruby on Rails 2.1.x - Preliminary

previous next

Advertisements

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:

Ruby is :

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:

Why Not Ruby ?

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
  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 following result:

Hello Ruby

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

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:

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," %>
<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>

previous next

Copyright © tutorialspoint.com