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

查看:113
本文介绍了什么是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

对数组排序很有用.

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

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