Ruby On Rails Introduction | Introduction to Ruby on Rails With Examples - W3Schools

What is Ruby?
There are a few things you should be aware of. The base for Rails is Ruby and that’s where we’ll begin with.

Ruby is a computer programming language that can be described as a combination of –
* Elegance of Smalltalk’s concepts,
* Ease of use and ease of learn from Python, and
* Perl’s pragmatism.

In simple words, Ruby is a high-level computer programming language which is a general-purpose, dynamic, interpreted (such as Perl, Python, Tcl/TK), and an object-oriented language (such as Smalltalk, Eiffel, Ada, Java).

Why Ruby Over Others?
Ruby possesses many useful features which make you choose it over others. Its origin lies in Japan, but it started gaining popularity in the US and Europe as well. The features which make it so popular are listed below –

* Easy to use
* Easy to learn
* Open source language
* Enriched libraries
* Extendable (easy to do so)
* Object-oriented (easy to understand)
* Limited coding and fewer chances of errors or bugs
* Owns helpful community

With all the above pros of Ruby, you should also be familiar with a few drawbacks of this language. There could be a chance that this language is not meant for you. So, consider the following drawbacks before you give this programming language a thumbs-up.

* Performance
Although being a popular competitor of Perl and Python, it is an interpreted language and that’s what makes it difficult to compare its performance with high-level languages like C, C++, Java, and many others.

* Threading Model
Ruby never uses native threads. Its threads are simulated in VM which is another drawback of this popular language.

Sample Ruby Code
The following sample of the Ruby code will print “Hello Ruby !” on the console –

# 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

Output
The final output of the above code will be –

Hello Ruby !
 
How to Embed Ruby (ERB) Code in an HTML File?
Ruby offers a program by the name ERB (Embedded Ruby). This program was written by Seki Masatoshi. The program makes Ruby codes capable of being embedded inside an HTML file. ERB starts by reading the code, word by word, and the time when it encounters the embedded Ruby code in the file, it executes that embedded Ruby code.

To prepare an ERB document, you should be knowing the following things –

* To execute the embedded Ruby code, it is important that you enclose the code between <% and %>.

* In case you want the result of the embedded Ruby code execution to be displayed on the console, as a part of the output, then enclose the code between <%= and %>.

The following example will help you understand the above two points. Here, we will be saving this code under the erbdemo.rb file. Always remember that .rb extension stands for a Ruby file.

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

To run this program, you need to enter the following command by making use of command-line utility erb –

tp> erb erbdemo.rb
 
The result of the above command will be –

<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 Ruby on Rails?
Ruby on Rails (sometimes referred as Rails or RoR too) is a server-side and open source web application framework. The framework is completely written in Ruby language. Like Python, Rails also follow the MVC (or Model-View-Controller) framework.

The following features will help you to understand about Rails more clearly –

* Extremely productive open source web application framework
* Developing a web application using Rails takes tenfold lesser time than that in a conventional Java framework
* Capable of developing database-backed web applications
* Database Schema lets you configure your code easily
* No need to go through the compilation phase

Full Stack Framework
* Framework contains everything which is required to create a database-driven web application
* The framework uses the Model-View-Controller (or MVC) pattern
* Full-stack framework refers to a layer based platform which makes it easy for developers to code
* concise coding required in comparison with other frameworks

Configuration
Ruby on Rails is much efficient when convention over configuration is considered.

* Rails persistently avoid configuration to support conventions, reflection, and dynamic runtime extensions
* The best part is that your running database and the application code carry everything which is required by the Rails.

Advantages of Using Ruby on Rails
With several interesting features, Ruby on Rails has become one of the most productive languages. Below are a few of its features behind its gaining popularity –

* Metaprogramming
Unlike other frameworks, Rails uses metaprogramming techniques which lets the developer skip the part in which they have to extensively generate their codes from scratch. Ruby as a programming language supports this feature excellently, thus, Rails also uses this capability of the framework very well. But that doesn’t mean Rails ignores code generation. It just relies on metaprogramming whenever heavy lifting comes into the picture.

* Active Record Framework
The Active Record framework makes it possible for the Ruby on Rails to store objects in a database. It begins with Active Record framework looking for the columns in the database schema. It then automatically attaches these columns to the objects of your domain. This is achieved with the help of metaprogramming.

* Convention over Configuration
There are several frameworks (such as .NET and Java) which make its developers write pages of configuration code. But with the help of suggested naming conventions, Rails spare you this lengthy and time consuming task.

* Scaffolding
To quickly build an application, a lot of users make use of temporary codes in the initial phase of the development of their web application. This is done just to see how the important components of the framework work together. This framework makes it easy for you by creating much of the scaffolding that you might require.

* Built-in Testing
Rails offers you a feature of creating basic automated tests which are easily extendable. There are two supporting codes provided by Rails, which are –

* harnesses, and
* fixtures.

Both of these supporting codes make the test cases easier to write and run. After which, Ruby can easily execute all of your automated tests with the rake utility.

* Three Environments
Ruby on Rails offers you 3 default environments, which are –

* development environment,
* testing environment, and
* production environment.

The working of each of these environments is slightly different from the others. This makes your complete software development cycle much easier. For an instance, Ruby on Rails keeps a fresh copy of the Test database after each run.