Copyright © tutorialspoint.com

Ruby on Rails Introduction

previous next


Advertisements

What is Ruby ?

Before we ride on Rails, let's know a little bit about Ruby which is the base of Rails.

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. Following are greatest factors:

Why Not Ruby ?

Sample Ruby Code:

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

   # The Hello Class
   class Hello
      def initialize( name )
         @name = name.capitalize
      end

      def salute
         puts "Hello #{@name}!"
      end
   end
   # Create a new object
   h = Hello.new("Ruby")
   # Output "Hello Ruby!"
   h.salute

Embedded 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.rb file. Please note that a ruby file will have extension .rb

<% 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.rb

This will produce 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

Full Stack Framework

Convention over Configuration

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 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 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.

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.

previous next

Copyright © tutorialspoint.com