[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

difference between @var and self.var

Shea Martin

6/21/2006 4:44:00 AM

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.
3 Answers

Timothy Goddard

6/21/2006 4:51:00 AM

0

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.

Jeff Schwab

6/21/2006 7:04:00 PM

0

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

A method.

> and @my_member?

A variable.

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

"self" is very similar to "this," or the "self" of Smalltalk. The need
of context to determine whether an identifier refers to a method or an
instance variable also occurs in C++:

struct B: A {
using A::i;
};

This is seldom a problem in C++ because no instance method may have the
same name as a member variable of the same class. In Java, this kind of
name-sharing is allowed, but methods are rarely referred to without
accompanying parenthesis. One obvious exception is the static import:

/* "i" might name a variable and/or one or more methods. */
import static pkg.A.i;

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

The my_member method may have the side effect of committing the new
value to a database. I don't know yet know as much as I would like
about Rails, though. :(

Shea Martin

6/22/2006 4:56:00 PM

0

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