C# ?? Ruby 中的运算符?

发布于 2024-07-22 07:32:37 字数 122 浏览 5 评论 0原文

是否可以实施? Ruby 中的运算符?

a = nil
b = 1

x = a ?? b # x should == 1
x = b ?? 2 # x should == 1

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

美煞众生 2024-07-29 07:32:37

在 Ruby 中,短路布尔运算符(||&&andor)不返回 true 或 false,而是返回确定整个表达式结果的第一个操作数。 这是可行的,因为 Ruby 对真理有一个相当简单的想法。 或者更确切地说,它有一个相当简单的错误概念:nil 是假的,显然 false 是假的。 其他一切都是正确的。

因此,由于当至少一个其操作数为真时||为真,并且操作数是从左到右计算的,这意味着a || 当 a 为 true 时,b 返回 a。 但当a为假时,则表达式的结果仅依赖于b,因此返回b

这意味着,由于 nil 为 false,因此您可以在给出的示例中使用 || 而不是 ?? 。 (还有一个漂亮的 a ||= b 运算符,其工作方式类似于 a || a = b,但不完全一样。)

但是, only 有效,因为您在示例中没有使用布尔值。 如果您希望处理布尔值,那么这是行不通的:

b = false

x = b || 2 # x should be == false, but will be 2

在这种情况下,您必须使用 #nil? 和条件表达式:

b = false

x = unless b.nil? then b else 2 end # x should be == 2

或使用三元条件运算符:

b = false

x = b.nil? ? 2 : b # x should be == false

如果您想要到,你可以用一个很好的方法来包装它:

class Object
  def _? b = nil
    return self
  end
end

class NilClass
  def _? b = nil
    return yield if block_given?
    return b
  end
end

b = false

x = b._? { 2 } # x should be == false
x = b._? 2 # x should be == false

这个可爱的片段是由多态性、开放类以及 nil 实际上是一个代表虚无的对象(与,比如说 Java,其中 null 实际上什么都没有。)

In Ruby, the short-circuiting Boolean operators (||, &&, and and or) do not return true or false, but rather the first operand that determines the outcome of the entire expression. This works, because Ruby has a rather simple idea of truth. Or rather, it has a rather simple idea of falsehood: nil is false, and obviously false is false. Everything else is true.

So, since || is true when at least one of its operands is true, and operands are evaluated from left to right, this means that a || b returns a, when a is true. But when a is false, then the outcome of the expression is solely dependent on b, and thus b is returned.

That means that, because nil is false, you can just use || instead of ?? for the examples that you gave. (There is also the nifty a ||= b operator, which kind of works like a || a = b, but not quite.)

However, that only works, because you don't use Booleans in your examples. If you expect to deal with Boolean values, that won't work:

b = false

x = b || 2 # x should be == false, but will be 2

In that case, you will have to use #nil?, and a conditional expression:

b = false

x = unless b.nil? then b else 2 end # x should be == 2

or using the ternary conditional operator:

b = false

x = b.nil? ? 2 : b # x should be == false

If you want to, you can wrap that up in a nice method:

class Object
  def _? b = nil
    return self
  end
end

class NilClass
  def _? b = nil
    return yield if block_given?
    return b
  end
end

b = false

x = b._? { 2 } # x should be == false
x = b._? 2 # x should be == false

This cute snippet brought to you by polymorphism, open classes and the fact that nil is actually an object representing nothingness (unlike, say, Java, where null is actually nothing).

天邊彩虹 2024-07-29 07:32:37

您正在寻找条件赋值:

a ||= b  # Assign if a isn't already set

和 || 操作员

a = b || 2 # Assign if b is assigned, or assign 2

You're looking for conditional assignment:

a ||= b  # Assign if a isn't already set

and the || operator

a = b || 2 # Assign if b is assigned, or assign 2
若水微香 2024-07-29 07:32:37
x = b || 2

它(C# 中的 ??)称为合并运算符。

x = b || 2

It (?? in C#) is called the coalesce operator.

离鸿 2024-07-29 07:32:37

coalesce gem,它是您所能得到的最接近的。

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

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

nil || 5 # => 5
false || 5 # => 5 :(
false._? 5 # => false :)
絕版丫頭 2024-07-29 07:32:37

Ruby 有一个空合并运算符 &.,称为“安全导航运算符”:

irb(main):004:0> a = nil
=> nil
irb(main):005:0> a&.foo
=> nil
irb(main):006:0> a = Struct.new(:foo).new(foo: 3)
=> #<struct  foo=3>
irb(main):007:0> a&.foo
=> 3

这并不完全是您所要求的,但这个问题在 google 上的“ruby null合并运算符”排名如此之高值得一提的是。

Ruby has a null coalescing operator &., called the "Safe Navigation Operator":

irb(main):004:0> a = nil
=> nil
irb(main):005:0> a&.foo
=> nil
irb(main):006:0> a = Struct.new(:foo).new(foo: 3)
=> #<struct  foo=3>
irb(main):007:0> a&.foo
=> 3

It's not exactly what you're asking for, but this question ranks so high on google for "ruby null coalescing operator" that it's worth mentioning here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文