11/09/05
Introduction to the Ruby language (and Rails) -
Categories: Ruby, Rails -
Serge Baccou
@ 11:01:27 pm
![]() |
Following my latest note Ruby on Rails: where to start with, I want to highlight some (good) characteristics of Ruby and also Rails, the Web development framework based on Ruby. |
Ruby is a pure object-oriented language
Like Smalltalk, Ruby is a pure object-oriented language. We will develop this afterwards.
Almost everything is an object in Ruby
For example, 3 is an instance of the Fixnum class. The following piece of code will display Hello 3 times.
3.times {
puts "Hello"
}
Messages vs methods
Consider a instance p of the class Person. The notation p.display() will throw the message display to the p instance. The p instance will look if there is a corresponding method and in that case it will run the methods (with the suitable arguments). The distinction between messages and methods is very useful to build a proxy object for example (to catch every messages and to send them to another object).
Duck typing
Ruby supports what we call Duck typing. Following the sentence "if it walks like a duck, and talks like a duck, then it might as well be a duck", an object can be considered as a different class as the one it belongs to since it has the same methods (here walk() and talk()). So Donald can be seen as a duck even if he is an instance of DisneyCharacter.
Object can be extended at runtime
In Ruby, you can dynamically add a new method to a particular instance of a class (and only to that instance if you want to).
Code should be as simple as possible
For example, in Ruby, there is no semicolumn at the end of each line of code.
Less code, less characters to type
For example, instance variables do not require $this->memberData or something like that. You will only have to write @memberData (@ is the character to gain access to instance variables).
Local, instance, global variables and constants
This is the Ruby notation for local variables, instance variables, global variables and constants (note that something that starts with a capital letter will be considered as a constant!):
localVariable
@instanceVariables
$globaVariables
CONSTANT1 or Constant2
Code must be programmer-friendly
For example, the following Ruby notation is used for methods that answer a question:
task1.isFinished?
Code evaluation
A Ruby program can produce Ruby code and can dynamically evaluate it! This is more difficult to do with let say PHP (you have to write the code into a file and include the generated file).
In Rails, the MVC model respected
Rails follows the Model-View-Controler architecture. The controller will receive the user requests (from a browser), the model will enrich the database representation with calculated attributes and the view will deal with the presentation stuff (in HTML for Rails). In Java, different component are necessary to follow the MVC architecture (Struts, Hibernate, JSF for example). In Rails, each part of the MVC architecture is using Ruby and is proposed by Rails.
Rails' Active Records
Active Records is the Ruby way to implement object mapping. Object mapping is the technique that automatically associate an object to a part of a database. With Active Records, you do not care of writing / reading in a database. You can just access to attributes of an object that will in fact read a column of a table in the database. This reduces dramatically the number of lines of code to write. In Java, Hibernate deals with the same feature.
Rails: conventions vs configuration files
In Rails, there is only one configuration file with only the parameters about the database. The rest is only conventions rather than configuration files.
You should also hake a look on the following documents:
- Programming Ruby - The Pragmatic Programmer's Guide
- 10 Things Every Java Programmer Should Know About Ruby (OSCON 2005)
- Ruby QuickRef
You should also watch the 2 hours RUC video about Rails.
Comments:
Leave a comment:
Pingbacks:
No Pingbacks for this post yet...
