variable access through self

Discussion in 'Ruby' started by Michael P. Soulier, Nov 1, 2006.

  1. --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 <>
    "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--
    Michael P. Soulier, Nov 1, 2006
    #1
    1. Advertising

  2. Michael P. Soulier

    John Wilger Guest

    On 10/31/06, Michael P. Soulier <> 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://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
    John Wilger, Nov 1, 2006
    #2
    1. Advertising

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

    > On 10/31/06, Michael P. Soulier <> 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://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
    Rob Biedenharn, Nov 1, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    552
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    570
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    383
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    353
    falcon
    Jul 31, 2005
  5. Bart Kastermans
    Replies:
    6
    Views:
    377
    Bart Kastermans
    Jul 13, 2008
Loading...

Share This Page