What I want to do is something like variable || "default string"
variable || "default string"
.
But this works only if variable
is nil and for some reason it's undefined.
Is it possible to set the default value to nil, or is there a better way to achieve this?
您可以利用Ruby局部变量默认为nil
的事实来获利:
variable ||= 'default string'
您可以这样做:
defined?(variable) ? variable : "default string"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.