[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

variable access through self

Michael P. Soulier

11/1/2006 4:52:00 AM

Hi,

I've seen a lot of examples in Rails code where they're accessing what appear
to be instance variables from self. Something like

self.foo = true

Now, I'm assuming that you can only access methods this way, so this is
actually a method call of

self.foo=(true)

Is that right? I'm not always clear on it.

Thanks,
Mike
--
Michael P. Soulier <msoulier@digitaltorque.ca>
"Any intelligent fool can make things bigger and more complex... It
takes a touch of genius - and a lot of courage to move in the opposite
direction." --Albert Einstein
2 Answers

John Wilger

11/1/2006 5:09:00 AM

0

On 10/31/06, Michael P. Soulier <msoulier@digitaltorque.ca> wrote:
> I've seen a lot of examples in Rails code where they're accessing what appear
> to be instance variables from self. Something like
>
> self.foo = true
>
> Now, I'm assuming that you can only access methods this way, so this is
> actually a method call of
>
> self.foo=(true)
>
> Is that right? I'm not always clear on it.

That is correct.

--
Regards,
John Wilger
http://john...

-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don't know," Alice answered.
"Then," said the cat, "it doesn't matter."
- Lewis Carrol, Alice in Wonderland

Rob Biedenharn

11/1/2006 1:55:00 PM

0

On Nov 1, 2006, at 12:09 AM, John Wilger wrote:

> On 10/31/06, Michael P. Soulier <msoulier@digitaltorque.ca> wrote:
>> I've seen a lot of examples in Rails code where they're accessing
>> what appear
>> to be instance variables from self. Something like
>>
>> self.foo = true
>>
>> Now, I'm assuming that you can only access methods this way, so
>> this is
>> actually a method call of
>>
>> self.foo=(true)
>>
>> Is that right? I'm not always clear on it.
>
> That is correct.
>
> --
> Regards,
> John Wilger
> http://john...
>
> -----------
> Alice came to a fork in the road. "Which road do I take?" she asked.
> "Where do you want to go?" responded the Cheshire cat.
> "I don't know," Alice answered.
> "Then," said the cat, "it doesn't matter."
> - Lewis Carrol, Alice in Wonderland

And you're also apparently a bit confused about what's really happening.

self.foo = true
becomes:
self.foo=(true)
which typically implies something like:

def foo=(foo)
@foo = foo
end

But you probably are more used to seeing something like:

attr_accessor :foo

which sets up the "def foo;@foo;end" and "def foo=(foo);@foo=foo;end"
for you.

-Rob

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