什么是 Ruby <=>(宇宙飞船)操作员? [英] What is the Ruby <=> (spaceship) operator?

查看:29
本文介绍了什么是 Ruby <=>(宇宙飞船)操作员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是 Ruby <=>(宇宙飞船)操作符?运算符是否由其他语言实现?

What is the Ruby <=> (spaceship) operator? Is the operator implemented by any other languages?

推荐答案

Perl 可能是第一种使用它的语言.Groovy 是另一种支持它的语言.基本上不是返回 1 (true) 或 0 (false) 取决于参数是否相等,飞船运算符将返回 10-1,具体取决于左参数相对于右参数的值.>

Perl was likely the first language to use it. Groovy is another language that supports it. Basically instead of returning 1 (true) or 0 (false) depending on whether the arguments are equal or unequal, the spaceship operator will return 1, 0, or −1 depending on the value of the left argument relative to the right argument.

a <=> b :=
  if a < b then return -1
  if a = b then return  0
  if a > b then return  1
  if a and b are not comparable then return nil

对数组进行排序很有用.

It's useful for sorting an array.

这篇关于什么是 Ruby &lt;=&gt;(宇宙飞船)操作员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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