difference between @var and self.var

Discussion in 'Ruby' started by Shea Martin, Jun 21, 2006.

  1. Shea Martin

    Shea Martin Guest

    I have been using Ruby for a while, and thought I had a pretty good
    grasp of the language, but...

    In an instance method, what is difference between self.my_member and
    @my_member? I though 'self' was like 'this' in C++ or Java, but it
    appears this is not the case. In my class, it seems possible to have
    different values for the two variation.

    ~S

    ps - this was actually seen in my Rails app, in the model. The member
    variable was one inherited from AR. I was setting it's value like this:
    @my_member = "sdfsdf", but the my_member column was not reflecting the
    change, until I used self.my_member = "asdf". This was in an instance
    method, not class method.
    Shea Martin, Jun 21, 2006
    #1
    1. Advertising

  2. Remember that in Ruby that you cannot directly access the instance
    variables of an object. When you call a.b you're really calling the 'b'
    method on a. These methods are generated by the attr functions. self is
    the current object, so you can only call methods on it. @var is the
    actual instance variable while self.var is the result of calling the
    var method.

    Shea Martin wrote:
    > I have been using Ruby for a while, and thought I had a pretty good
    > grasp of the language, but...
    >
    > In an instance method, what is difference between self.my_member and
    > @my_member? I though 'self' was like 'this' in C++ or Java, but it
    > appears this is not the case. In my class, it seems possible to have
    > different values for the two variation.
    >
    > ~S
    >
    > ps - this was actually seen in my Rails app, in the model. The member
    > variable was one inherited from AR. I was setting it's value like this:
    > @my_member = "sdfsdf", but the my_member column was not reflecting the
    > change, until I used self.my_member = "asdf". This was in an instance
    > method, not class method.
    Timothy Goddard, Jun 21, 2006
    #2
    1. Advertising

  3. Shea Martin

    Shea Martin Guest

    Jeffrey Schwab wrote:

    >> In an instance method, what is difference between self.my_member

    >
    > A method.
    >
    >> and @my_member?

    >
    > A variable.


    Thanks, it seems dead obvious now. :-(


    I don't know yet know as much as I would like
    > about Rails, though. :(


    <rant>
    Well it is worth learning. I have done dynamic sites in PHP before, but
    doing this site with Rails makes me giddy. It is really the same
    elation as learning ruby. Ruby made programming feel less like work,
    and more like play. Rails actually made web programming fun (something
    I thought would never happen again).
    </rant>

    ~S
    Shea Martin, Jun 22, 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. thomson
    Replies:
    10
    Views:
    2,482
    Eliyahu Goldin
    Jun 20, 2005
  2. thomson
    Replies:
    0
    Views:
    366
    thomson
    Jun 20, 2005
  3. daniele.g
    Replies:
    4
    Views:
    476
    Joe Pfeiffer
    Jul 15, 2011
  4. Replies:
    17
    Views:
    182
  5. erdibalint
    Replies:
    7
    Views:
    96
    Evertjan.
    Feb 21, 2008
Loading...

Share This Page