Is there a difference between self.current_user and @current_user?
I am looking at this ruby code and they make reference to:
@current_user
and
self.current_user
what is the difference?
http://code.goog开发者_C百科le.com/p/openplaques/source/browse/trunk/www/lib/authenticated_system.rb
@current_user
is an instance variable. self.current_user
calls the method on line 10 that returns that instance variable, first populating it if it is currently nil.
@current_user
accesses the actual property of the object whereas self.current_user
is calling the current_user
method on self
.
This means you could do something like this:
def current_user
@current_user.first_name
end
So now accessing @current_user
will still give you the property but self.current_user
will give you back the first name only.
In your specific example they are using object caching to set the @current_user
property only the first time it is accessed. This means that if @current_user
is nil, it'll do (login_from_session || login_from_basic_auth || login_from_cookie)
otherwise it'll just return the existing object without reinitializing it.
@current_user
dereferences the instance variable called @current_user
.
self.current_user
sends the message :current_user
to self
.
精彩评论