Simply Ruby on Rails

shoulderscobblerInternet and Web Development

Feb 2, 2013 (4 years and 6 months ago)

100 views

RUBY ON RAILS
--

Web Development Platform that doesn’t Hurt



Before starting about Ruby on Rails, it’s important that we Understand a little about what
ruby language is all about on which ROR (Ruby on Rails) is based.
Ruby

is a
reflective
,
object
-
orient
ed

programming language
. It combines syntax inspired by
Perl

with
Smalltalk
-
like object
-
oriented features, and also shares some features with
Python
. Ruby
is a single
-
pass
interpreted language
.
A
One
-
pass
interpreter

is a type of software
interpreter

that
passes through the
source code

of each compilation unit only once. In a
sense, a one
-
pass compiler can't 'look back' at code it previously processed.
The ability to
compile in a
single pass

is often seen as a benefit because it simplifies the job of writin
g a
compiler and one pass compilers are generally faster than
multi
-
pass compilers
.
The
Ruby official distribution also includes "
ruby
", an interactive command
-
line interpreter
which can be used to test code quickly.


Now let’s move to understand what is
ruby on rails. ROR is an
open source

web
application framework

written in
Ruby

that closely follows the
Model
-
View
-
Controller

(MVC) architecture. It strives for simplicity and allowing real
-
world applications to be
developed in less code than other framewo
rks and with a minimum of configuration. I
would like to put a line by someone who used ROR. He said ‘
Coming from PHP and Java, I
remember how many of Ruby’s most wonderful features seemed odd at first. “What is it exactly that
makes blocks so special?” I
thought. “They’re just convenience for writing a line of code at the
beginning and the end.” Little did I know... As I started using Ruby and extracting Rails, I quickly
wised up. Ruby is such an incredibly rich and expressive language that it’s hard to ap
preciate its
beauty by simply relating it to past experiences with other
languages.’
Ruby on rails is framework
written entirely in Ruby
.
So writing application using ROR means you are using Ruby as
language
.


Now let’s see in programming sense about how R
uby works:


A First Ruby Program
:


c = 100

f = (c * 9 / 5) + 32

puts "The result is: "

puts f

puts "."



Let’s save this prog with name c2f.rb, the file extension for ruby is rb. To check syntax
for program, we can do it like:



ruby
-
cw c2f.rb


If syntax
is correct it will says
Syntax OK


To run the program:



ruby c2f.rb

This will give the required output.


“load”
-
ing a file

A close relative of require is load. The main difference is that if you do this

require "requiree.rb"

require "requiree.rb"

nothing
happens the second time; whereas if you do this

load "requiree.rb"

load "requiree.rb"

Ruby reads in the file twice.

Doing it twice in a row in the same file is almost certainly pointless, but in some

Cases this kind of multiple loading is useful. Rails use
s load
in preference to
require, for
example, in development mode

which means that if you’re trying

your application in a
browser and making changes to the code at the same time,

your changes are reloaded,
overriding any caching behavior on the part of the

Web server.


When you install Ruby on your system, you really install several layers. First is the
core
Ruby language:
the basic operations and programming techniques available when you
run the Ruby interpreter. Second are a large number of
extensions
and

programming
libraries
bundled with

Ruby

add
-
ons that help you write programs in different areas of
specialization. These are usually referred to collectively as the
standard library
. Ruby
comes with extensions for a wide variety of projects and tasks: dat
abase management,
networking, specialized mathematics, XML processing, and many more.


For using any extension we need to use
require '
ExtensionName
'
, it similar
to what we do in C# by using Using Keywork


The site_ruby directory (sitedir) and its subdirec
tories (sitelibdir, sitearchdir)
.
Your Ruby
installation
include

a subdirectory called site_ruby. As its name suggests
,
site_ruby is
where you a
nd/or your system administrator
store third
-
party extensions and libraries.

When you require an extension, the Ru
by interpreter checks for it in these subdirectories
of site_ruby as well as in both the main rubylibdir and the main archdir.


The gems directory


This directory is installed in same directory as site_ruby if we install ruby on rails too.


ERb

ERb
allows
you to put Ruby code inside an
HTML
file and
ERb
interprets the whole thing as program
input.


A Simple example


<% page_title = "Demonstration of ERb" %>

<% salutation = "Dear programmer," %>

<html>

<head>

<title><%= page_title %></title>

</head>

<body>

<p><%= salutation %></p>

<p>This is a brief demonstration of how ERb fills out a template.</p>
</body></html>

Lets save this as demo.rb, and run erb demo.rb and you will get following output


<html>

<head>

<title>Demonstration of ERb</title>

</head>

<body>

<p>Dear programmer,</p>

<p>This is a brief demonstration of how ERb fills out a template.</p>

</body>

</html>


ROR follows MVC framework concept,
to see
MVC
working lets see ruby example


Run the following command from a directory in which you’d like to pl
ace the sample Rails
application directory:

$ rails test


The program
rails
, which is installed with the Rails framework, performs the task of creating an
application directory

in this case, a directory called
test
. If you look inside the
app
subdirectory,

you’ll see (among other things) subdirectories called
models
,
controllers
, and
views
. The
relevant model and controller program files and view templates will reside in these subdirectories.

The
MVC
concept is all about dividing the work of programming and

the functioning of a program into
three layers: model, view, and controller. The rails is all about helping with that. Below is a simple
diagram taken from internet.



So from here what I conclude is ROR is set of libraries like .NET or J2ee, all called
frameworks. The
things we need to learn for ROR are Rails framework like
it’s

for .NET, I will below compare it with
.NET libraries





ROR

.NET

Action Controller and Action View

Similar to asp.net library not sure though.

ACTIV
E RECORD


ADO.NET

Support Libraries

Base libraries



So these are libraries which we need to get familiar with to learn ROR.