variable access through self

  • Thread starter Michael P. Soulier
  • Start date
M

Michael P. Soulier

--PNftq4k6NuHCGvy8
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hi,

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

self.foo =3D true

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

self.foo=3D(true)

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

Thanks,
Mike
--=20
Michael P. Soulier <[email protected]>
"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

--PNftq4k6NuHCGvy8
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFFSAQhKGqCc1vIvggRAoCLAKCZyNOv2K6Y68z/5LQnp7iDNNOK6QCdHVbW
Onfvzw1rNJqWOh5T74eeyVg=
=Y3+k
-----END PGP SIGNATURE-----

--PNftq4k6NuHCGvy8--
 
J

John Wilger

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://johnwilger.com

-----------
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
 
R

Rob Biedenharn

That is correct.

--
Regards,
John Wilger
http://johnwilger.com

-----------
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://agileconsultingllc.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

alarm? 4
ANN: Tftpplus 0.4 0
using common layout in Rails 3
undefined method `recvfrom_nonblock' 9
FileUtils.cp 4
receive timeout on socket 0
global error handling in rails 0
IO#reopen bug in 1.8.4? 1

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top