Sie sind auf Seite 1von 5

A Primer on Ruby on Rails – Part 1 Mohamad Haj Hasan

Model-View-Controller (MVC) Architecture

MVC has been around since the late 70s, but Ruby on Rails (RoR) is the first time that it is used
on such a large-scale web application framework.

• Model

This is basically what is controlling the data. The actual database that stores the data itself
can be from a number of different places (such as a database, a web API of another
website’s data, on local hard disc, etc. – basically any store of data). The model can even
query a payment system. In addition to reading data, the model can also write data. The
model is the glue, but it must be kept relatively slim without too much code or logic.

• View

This is basically what you “see” as the end user of a web application. It could be HTML,
XML (for web services and RSS feeds), JavaScript or CSS.

• Controller

This is the “middle man”. The controller takes data from the model and passes it into the
appropriate view. It keeps everything organized.

Figure 1. MVC Framework

1|Page
A Primer on Ruby on Rails – Part 1 Mohamad Haj Hasan

Why RoR?

RoR offers a lot of great advantages, some of which are:

1. Good organization

RoR is opinionated software, which means that it makes small decisions on your behalf.
RoR organizes folders that look the same across different projects (e.g. app, views, helpers,
etc.).

2. Good practices

RoR is based on test-driven development, which tries to minimize bugs over the long run.

3. Good tools

RoR has a good set of support libraries called gems as well as specific RoR code called
plugins (which are sets of functionality that are written by others that you can use “off the
shelf” that save you the time of re-writing the functionality all over again). RoR also has
other useful tools such as the “rake” tool, which is great for system administration.

4. Great (and active) community

5. It makes YOU a better programmer

Object-oriented Languages

RoR is an object-oriented language; everything is an “object”. For a better understanding, take


this example:

In the real world, you often have many “objects” of the same kind. For example, your bicycle is
really just one of many bicycles in the world. Using object-oriented terminology, we say that your
bicycle is an “instance” of the “class” of “objects” known as bicycles. All bicycles have some
states (current gear, current motion, two wheels) and behaviors (change gears, brake) in
common.

When building bicycles, manufacturers take advantage of the fact that bicycles share some
characteristics, and they build many bicycles from the same blueprint. It would be very
inefficient to produce a new blueprint for every individual bicycle they manufactured.

In object-oriented software, it's also possible to have many objects of the same kind that share
characteristics: rectangles, employee records, video clips, and so on. Like the bicycle
manufacturers, you can take advantage of the fact that objects of the same kind share certain
characteristics and you can create a blueprint for those objects. Software blueprints for objects
are called classes. A class is a template or prototype that defines the variables and the
methods (or functions) common to all objects of a certain kind.

2|Page
A Primer on Ruby on Rails – Part 1 Mohamad Haj Hasan

For example, you can create the bicycle class that would declare several variables to contain
the current gear, the current motion, etc. It would also declare and provide implementations for
the methods that allow the rider to change gears or brake. Classes provide the benefit of
reusability. Bicycle manufacturers reuse the same blueprint over and over again to build lots of
bicycles. Software programmers use the same class over and over to again create many
objects of the same class.

Basics of RoR

RoR is a very powerful tool. Below are some of the important basic components:

• The “irb”

This is the interactive ruby interpreter. The interpreter runs the RoR script, which essentially
means that it runs the RoR code and prints to the screen when you press “enter”. You type
“irb” to enter the irb and “exit” to exit the irb.

• Objects

EVERYTHING in RoR is an object. This means that you can run methods (with the “.”
symbol) on just about everything. For example:

“Roger”.length => will give you a result of 5 (i.e. 5 letters).

string method

• Methods

Methods are things you “do” to objects. RoR has methods that are built into every object that
you can work with and make use of. You can also write your own methods.

• Object consistency

You CANNOT work with two different objects at the same time, you MUST convert one
object type to another. For example:

“1” + 2 => does not work.

string integer

“1”.to_i + 2 => works! You converted the string to an integer with a method.

string to integer integer


3|Page
A Primer on Ruby on Rails – Part 1 Mohamad Haj Hasan

• The symbol

One very confusing object in RoR is the symbol, or “:symbol”. The :symbol is basically just a
lightweight string that is often used as a key for hash tables, :symbols just save a little
memory.

• Variables (local or otherwise)

By definition, variables are things that change. You can execute methods on variables
based on what kind of object the variables are pointing to. You can also put variables inside
strings, like this:

“My name is #{name}”

“name” is a variable that is put inside the “#{ }”

• Instance variables

These are variables that start with the “@” sign. They work just like normal variables, but are
used with classes. So the multiple methods in a class can access these instance variables.
Instance variables are usually used inside controllers to pass information back and forth
between controllers and views.

• Arrays

An array is a valuable data structure and is essentially an ordered list. Take for example the
following array:

array = [5, 9, 3]

array[0]

You can use methods on arrays, so:

array.last => gives you 3

• Hash tables

Hash tables are “key” and “value” structures that are not ordered, but the maintain a
relationship. So for example:

hash = {:fish => “salmon”, :pet => “dog”}

key value

4|Page
A Primer on Ruby on Rails – Part 1 Mohamad Haj Hasan

You can set values in hash tables like this:

hash[:fish] = “tuna”

You can also add hash values like this:

hash[:beef] = “angus”

• Blocks

Blocks loop through things and can run snippets of code inside other code. Take for
example the following:

array.each do |a|

puts a => this function prints all the elements within the array.

end1

Another useful block function is the “each_with_index” function, for example:

array.each do |a, i| => you are taking two arguments here.

puts “#{a} is number #{i}”

end

1
Another (advanced) way of writing this statement is: “array.each { |a| puts a }” with no “do” or “end”.

5|Page

Das könnte Ihnen auch gefallen