本想google, 但不知道这样的用法怎么去搜索,只能在这问了。
代码:
def current_user
@current_user ||= login_from_session
end
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
如果这样写在多行代码中,确实是
空指针保护但在你的问题中,整个方法中只有这一行,这个技巧应该叫
Caching with Instance Variables,为了在多次调用这个方法时提高性能这是 Ruby 程序员的常用方法:
空指针保护。