Beruflich Dokumente
Kultur Dokumente
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.
1|Page
A Primer on Ruby on Rails – Part 1 Mohamad Haj Hasan
Why RoR?
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.
Object-oriented Languages
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:
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:
string integer
“1”.to_i + 2 => works! You converted the string to an integer with a method.
• 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.
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:
• 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]
• Hash tables
Hash tables are “key” and “value” structures that are not ordered, but the maintain a
relationship. So for example:
key value
4|Page
A Primer on Ruby on Rails – Part 1 Mohamad Haj Hasan
hash[:fish] = “tuna”
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
end
1
Another (advanced) way of writing this statement is: “array.each { |a| puts a }” with no “do” or “end”.
5|Page