Hi Raul,
This is so funny to read, you're so good in explain the technical stuff in a
very entertaining way. Enjoy reading it and learn more
about self, left and right side of the "=" sign. Excellent!!
Mee
2008/9/25 raul parolari <[EMAIL PROTECTED]>
>
> > Yes, that makes sense. x is nil, so I can't invoke method on it. But in
> our case 'name' was not null,
>
> First, let's put this code in a simple class, to test it:
>
> class A
> attr_accessor :name
>
> def initialize(name)
> @name = name
> end
>
> def process_name_ok
> self.name = name.gsub(/[^A-Za-z]/, "")
> end
>
> def process_name_notok
> name = name.gsub(/[^A-Za-z]/, "")
> end
>
> end
>
> a = A.new("123Pawell")
> p a.process_name_ok
>
> # => "Pawell"
>
> # Now, let's call the bad guy, to make sure we recreated the bug:
> begin
> p a.process_name_not_ok
> rescue Exception => e
> puts "Uups! #{e}"
> end
>
> # => Uups! undefined method `process_name_not_ok' for #<A:0x542f90
> @name="Pawell">
> --------
>
> Ok, now let me play the part of the MRI (Matz Ruby Interpreter) executing
> the bad code (find his words in the comments :-):
>
> # ... I am given this line inside a method:
> name = name.gsub(/[^A-Za-z]/, "")
>
> # I see a 'name' on the left side; I remember the day when I was still
> young
> # and I went with Matz to that Japanese garden, when he told me to first
> # look for local variables, then for methods (I still remember that gentle
> # afternoon, how times have changed! now everyone wants to do a better
> # version of me... but I must be strong and accept it).
> # Ok, as I don't have a local variable 'name' in this method, the user is
> clearly
> # creating one and assigning to it; ok, there! I put a local variable
> 'name' in
> # my symbol table for this method, and I will initialize it to nil, while
> # waiting to see what the user will do on the right side.
> # But first let's take a little break for everyone to think about it...
>
> # All right, I am the MRI and I am back! let's look at the right side:
> name.gsub(/[^A-Za-z]/, "")
>
> # Aha, I see a 'name' on the right side; as Matz taught me, first I look
> for
> # variables: do I have a 'name' variable? oh, yes, there is a 'name'
> variable
> # in the little symbol table for this method, and its value is nil. Let's
> use it!
> # Oh, my God! the user asks to run a gsub on a nil value... KABOOOMM!
> -----
>
> Does it make sense? :-)
>
> Raul
>
>
> On Thu, Sep 25, 2008 at 3:43 PM, Paweł Stawicki <[EMAIL PROTECTED]>wrote:
>
>> >
>> > First, let's do something very simple in irb:
>> >
>> > x = x + 1
>> >
>> > Do you see what happened? you are trying to assign a value to a variable
>> > that you just created (on the left side) which is for the moment nil, so
>> ...
>> > does it make sense?
>> >
>> > And this is exactly what happens when you wrote.
>> > name = name.gsub...
>> > Do you see it now? (if not, don't be afraid to ask again; I am sure that
>> > your questions are shared by many).
>>
>> Yes, that makes sense. x is nil, so I can't invoke method on it. But
>> in our case name was not null, I was able to invoke gsub on it, it
>> only depended on "self." on the left side, while method was invoked on
>> the right side.
>>
>> >> If you could also explain how ruby can think "name" on the left side is
>> >> method. It has "=" sign on the right, is it possible with methods?
>> >
>> > Ah... but the method would be not 'name' but 'name='. In the code that
>> we
>> > discussed, Ruby has to decide if you are creating a local variable
>> 'name' or
>> > calling the accessor name= (that for the reasons discussed you should
>> write
>> > 'self.name=').
>>
>> And space sign doesn't matter? There is no difference between "name="
>> and "name ="? Well, if I can call
>> object.name = "smth"
>> and
>> object.name="smth"
>> and it is the same, I think there is no difference. Ah, I like to
>> answer to my own questions :)
>>
>> > It is always a pleasure to hear your questions (they force me to think,
>> > before answering). Do not hesitate if you have more,
>>
>> Thanks. I also like your responses very much. You are very helpful and
>> it is very good to have such person in this group.
>>
>> 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
-~----------~----~----~----~------~----~------~--~---