我在看这个Ruby代码和他们做参考:有self.current_user和@current_user之间的差异?
@current_user
和
self.current_user
的区别是什么?
http://code.google.com/p/openplaques/source/browse/trunk/www/lib/authenticated_system.rb
我在看这个Ruby代码和他们做参考:有self.current_user和@current_user之间的差异?
@current_user
和
self.current_user
的区别是什么?
http://code.google.com/p/openplaques/source/browse/trunk/www/lib/authenticated_system.rb
@current_user
是instance variable。 self.current_user
调用返回该实例变量在第10行的方法中,第一填充它,如果它是目前NIL。
@current_user
访问而self.current_user
呼吁self
的current_user
方法的对象的实际属性。
这意味着你可以做这样的事情:
def current_user
@current_user.first_name
end
所以现在访问@current_user
仍然会给你的财产,但self.current_user
会给你回的只有名字。
在你的具体的例子,他们正在使用对象缓存设置@current_user
属性仅在第一次被访问。这意味着,如果@current_user
是零,它会做(login_from_session || login_from_basic_auth || login_from_cookie)
否则只会返回现有对象,而无需重新初始化。
@current_user
取消引用名为@current_user
的实例变量。
self.current_user
将消息发送到:current_user
self
。