[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

in the callback why I must use self

Raj Singh

5/30/2008 1:19:00 PM


class User < ActiveRecord::Base
def before_save
self.name1 = fname + ' ' + lname
name2 = fname + ' ' + lname
end
end


In the callback I am using self for name1 and NOT using self for name2.
When a record is saved in the database I get proper value for name1 but
not for name2. Question is why?


In my judgement before name2 whether I use self or not should not matter
because there is an implicit self when there is no receiver. Then why
the difference in behavior?
--
Posted via http://www.ruby-....

5 Answers

Jason Roelofs

5/30/2008 1:32:00 PM

0

On 5/30/08, Raj Singh <neeraj.jsr@gmail.com> wrote:
>
> class User < ActiveRecord::Base
> def before_save
> self.name1 = fname + ' ' + lname
> name2 = fname + ' ' + lname
> end
> end
>
>
> In the callback I am using self for name1 and NOT using self for name2.
> When a record is saved in the database I get proper value for name1 but
> not for name2. Question is why?
>
>
> In my judgement before name2 whether I use self or not should not matter
> because there is an implicit self when there is no receiver. Then why
> the difference in behavior?
>
> --
> Posted via http://www.ruby-....
>
>

The issue here is that the parser can't tell the difference between
calling the method #name2= or creating a new local variable named
"name2"

In this case, the proper course of action is to use "self.name2 = " to
make sure there is no confusion.

Jason

Rob Biedenharn

5/30/2008 2:19:00 PM

0

On May 30, 2008, at 9:32 AM, Jason Roelofs wrote:

> The issue here is that the parser can't tell the difference between
> calling the method #name2= or creating a new local variable named
> "name2"
>
> In this case, the proper course of action is to use "self.name2 = " to
> make sure there is no confusion.
>
> Jason


...and the reason Ruby can't has to do with the dynamic way that
ActiveRecord provides model attributes. Ruby can't introspect on the
object to find that name2= exists so it is quite happy with calling
name2 a local variable. If you used name2 on the first line
(self.name1 = fname + name2) then Ruby would have to assume that name2
is a method since it would otherwise be an undefined local variable.
(I don't know if it would then find name2= for the next line, but I
suspect not. That would be easy for you to try anyway.)

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



chuck

11/30/2010 5:36:00 PM

0

On Nov 30, 12:15 pm, "Mike M. in MI" <mikemug...@hotmail.com> wrote:
> On Nov 30, 11:43 am, "S. Fitz" <sys11...@gmail.com> wrote:
>
> >http://tinyurl.c...
>
> > The things people have stashed away!
>
> If I owned a Diner I would be all over that.
>
> Mike

It has serious insert issues. You would be better off having one
restored for half that price.

Retrogameconnection

12/1/2010 6:32:00 AM

0

On Nov 30, 9:28 am, "S. Fitz" <sys11...@gmail.com> wrote:
> On Dec 1, 4:15 am, "Mike M. in MI" <mikemug...@hotmail.com> wrote:
>
> > On Nov 30, 11:43 am, "S. Fitz" <sys11...@gmail.com> wrote:
>
> > >http://tinyurl.c...
>
> > > The things people have stashed away!
>
> > If I owned a Diner I would be all over that.
>
> > Mike
>
> If I had the spare $$ I`d buy it just to send it to CPR


I'm curious - Why ??


They've already said many times publicly that they are NOT accepting
any new play field artwork. They currently have "years" worth of work
before accepting anything new!

NM

12/1/2010 6:54:00 AM

0

On Nov 30, 11:36 am, chuck <ch...@clhess.com> wrote:

> It has serious insert issues. You would be better off having one
> restored for half that price.

Can you please educate us on what you're seeing?

From those four low-resolution shots, I tried zooming but the images
just distort.

Regards, Neil