【发布时间】:2021-05-25 07:28:11
【问题描述】:
在 kotlin 和 C# 中,您可以分配一个变量,否则如果值为 nil,您可以使用 ?: 和 ?? 运算符引发异常。
例如,在 C# 中:
var targetUrl = GetA() ?? throw new Exception("Missing A");
// alt
var targetUrl = GetA() ?? GetB() ?? throw new Exception("Missing A or B");
这在红宝石中可能吗?如果有,怎么做?
基本上,我想做的就是这个
target_url = @maybe_a || @maybe_b || raise "either a or b must be assigned"
我知道我可以做到这一点
target_url = @maybe_a || @maybe_b
raise "either a or b must be assigned" unless target_url
但如果可能的话,我想在一行中完成
【问题讨论】:
标签: ruby raiseerror