||= 是什么意思? [英] What does ||= mean?

查看:89
本文介绍了||= 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序控制器中有一个受保护的方法

I have a protected method in my application contoller

def current_user
  @current_user ||= User.find_by_id(session[:user_id])
end

我想知道 ||= 是什么意思?我一直试图寻找并找出答案,但无济于事.

I was wondering what ||= means? I've been trying to search and find out, but to no avail.

推荐答案

基本上,a ||= b 的意思是 assign b to a if a is null or undefined or false (ie false-ish value in ruby​​),它是a = b的快捷方式,除非a.

Basically, a ||= b means assign b to a if a is null or undefined or false (i.e. false-ish value in ruby), it is a shortcut to a = b unless a.

这篇关于||= 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆