C# ?? operator in Ruby?

前端 未结 4 1385
猫巷女王i
猫巷女王i 2020-12-16 09:05

Is it possible to implement the ?? operator in Ruby?

a = nil
b = 1

x = a ?? b # x should == 1
x = b ?? 2 # x should == 1
4条回答
  •  忘掉有多难
    2020-12-16 09:41

    There is the coalesce gem, which is as close as you'll get.

    nil || 5 # => 5
    false || 5 # => 5 :(
    false._? 5 # => false :)
    

提交回复
热议问题