• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
 
A Primer on Ruby on Rails – Part 1
Mohamad Haj Hasan
 
1 |Page 
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 usedon such a large-scale web application framework.
ModelThis 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 evenquery a payment system. In addition to reading data, the model can also write data. Themodel is the glue, but it must be kept relatively slim without too much code or logic.
ViewThis 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 theappropriate view. It keeps everything organized.
Figure 1.
MVC Framework 
 
A Primer on Ruby on Rails – Part 1
Mohamad Haj Hasan
 
2 |Page 
Why RoR?
RoR offers a lot of great advantages, some of which are:1. Good organizationRoR 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 practicesRoR is based on test-driven development, which tries to minimize bugs over the long run.3. Good toolsRoR has a good set of support libraries called gems as well as specific RoR code calledplugins (which are sets of functionality that are written by others that you can use “off theshelf” that save you the time of re-writing the functionality all over again). RoR also hasother useful tools such as the “rake” tool, which is great for system administration.4. Great (and active) community5. It makes YOU a better programmer 
Object-oriented Languages
RoR is an object-oriented language; everything is an “object”. For a better understanding, takethis example:In the real world, you often have many “objects” of the same kind. For example, your bicycle isreally 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 somestates (current gear, current motion, two wheels) and behaviors (change gears, brake) incommon.When building bicycles, manufacturers take advantage of the fact that bicycles share somecharacteristics, and they build many bicycles from the same blueprint. It would be veryinefficient 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 sharecharacteristics: rectangles, employee records, video clips, and so on. Like the bicyclemanufacturers, you can take advantage of the fact that objects of the same kind share certaincharacteristics and you can create a blueprint for those objects. Software blueprints for objectsare called
classes
. A class is a template or prototype that defines the variables and themethods (or functions) common to all objects of a certain kind.
 
A Primer on Ruby on Rails – Part 1
Mohamad Haj Hasan
 
3 |Page For example, you can create the bicycle class that would declare several variables to containthe 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 manyobjects 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
i
nteractive
u
b
y interpreter. The interpreter runs the RoR script, which essentiallymeans 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.
ObjectsEVERYTHING 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 
MethodsMethods are things you “do” to objects. RoR has methods that are built into every object thatyou can work with and make use of. You can also write your own methods.
Object consistencyYou CANNOT work with two different objects at the same time, you MUST convert oneobject 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 
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...