Hi, Pawel
> what is the difference between "self.name" and "name"? ..
let me first say that it is a very intelligent question (one that few dare
to ask, but that I bet is in the mind of many...).
The problem is the following: if you write '*name*' in the *left side* of an
expression, Ruby will think that it is a local variable that you are
assigning to (even if there is a method '*name*' available)...
Ruby is not so intelligent to say "*hmm... it could be either one, and as
this var is not being used after that, surely the programmer means to call
the method name!*" (this will be done in release 2.0... no, just joking :-).
Matz had to choose a fixed priority: it chose the local variable over the
method.
Bottom line: when you mean to call a method in the left side of an
expression, specify the receiver (in this case, self).
Raul
On Wed, Sep 24, 2008 at 4:08 PM, Paweł Stawicki <[EMAIL PROTECTED]>wrote:
>
> Hello,
>
> There is code in lab 5522 "ActiveRecord Basics":
>
> def before_validation_on_create
> self.name = name.gsub(/[^A-Za-z]/, "") if attribute_present?("name")
> end
>
> What is self here for? What is the difference between "self.name" and
> "name"?
>
> Best regards
> --
> Pawel Stawicki
> http://pawelstawicki.blogspot.com
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "ruby-on-rails-programming-with-passion" group.
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/ruby-on-rails-programming-with-passion?hl=en?hl=en
-~----------~----~----~----~------~----~------~--~---