How Ruby Works !!!

11 views
Skip to first unread message

Pavan @ Ruby On Rails

unread,
Sep 25, 2007, 2:09:35 AM9/25/07
to rubygems
Friends,
As we all are the member of this group, so certainly, we
are the RoR professionals or want to be.

The very important quiz is "How Ruby Works"....

Here, I am trying to put my best efforts to explain some unexplained
concepts...

1. A brief introduction to method calls and Ruby objects -->

Method calls sometimes consist simply of the name of a method, in
bareword form, possibly followed by one or more arguments to the
method. For example, this code calls the
method puts with one argument:

puts "Hello."

Other method calls use a special syntax: a dot operator, which
establishes a relationship between a value or expression to its left
and a method name to its right.

x = "100".to_i

the dot means that the message "to_i" is being sent to the string
"100", or that the
method to_i is being called on the string "100". The string "100" is
called the
receiver of the message.

Here's a method call that uses the full dot notation and also takes an
argument. This is a way to generate a decimal integer equivalent to
the base-nine number 100:

x = "100".to_i(9)

x is now equal to 81 decimal.

This example also shows the use of parentheses around method
arguments. These parentheses are usually optional, but in more complex
cases they may be required to clear up what might otherwise be
ambiguities in the syntax.
Many programmers use parentheses in most or all method
calls, just to be safe (and for visual clarity). In these examples,
the string "100" functions as the receiver of the message "to_i".
Basically, you're addressing the string with the request Convert
yourself to an integer. The string itself is an object. The whole
universe of a Ruby program consists of messages being sent to objects.
An object might be a string (as in the last example). It might be an
integer-perhaps an integer you want to convert to a string:

100.to_s

When you write a Ruby program, you spend most of your time either
telling Ruby what you want objects to be able to do-what messages you
want them to be able to understand-or sending messages to objects. Nor
are you limited in your object universe to things that Ruby already
knows about, like strings and integers. If you're writing a Rails
application in which one of your entity models is, say, Customer, then
when you write the code that causes things to happen-a customer
logging into a site, updating a customer's phone number, adding an
item to a
customer's shopping cart-in all likelihood you'll be sending messages
to customer
objects.
This brief sketch is just for Ruby literacy bootstrapping
purposes. When you see a dot
in what would otherwise be an inexplicable position, you should
interpret it as a message (on the right) being sent to an object (on
the left).


Regards,

Pavan Agrawal

Reply all
Reply to author
Forward
0 new messages