Baccou Bonneville Blogs Eclipse Blog Process Improvement Blog Java Blog Web Design Blog Miscellaneous .NET Blog

11/09/05

English (US)   Introduction to the Ruby language (and Rails)  -  Categories: Ruby, Rails  -  @ 11:01:27 pm
Ruby 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.

[More:]

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:

You should also watch the 2 hours RUC video about Rails.

Technorati tags: ruby
2 comments

Comments:

Comment from: Professional web design Specialist [Visitor]
i would like to learn Ruby. the program looks like C and Java...so i guess it's a good start:)
PermalinkPermalink 11/14/05 @ 08:35
Comment from: Serge Baccou [Member] Email · http://www.baccoubonneville.com
If you know Java, you should feel at home.
PermalinkPermalink 11/14/05 @ 08:51

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))
This is a captcha-picture. It is used to prevent mass-access by robots.

Please enter the characters from the image above. (case insensitive)

Pingbacks:

No Pingbacks for this post yet...

powered by
b2evolution

Credits: blog software | web hosting | monetize