什么是Ruby< => (宇宙飞船)运营商?

时间:2009-05-06 01:27:39

标签: ruby operators comparison-operators spaceship-operator

什么是Ruby <=>(宇宙飞船)运营商?运营商是否以其他语言实施?

6 个答案:

答案 0 :(得分:316)

Perl可能是第一种使用它的语言。 Groovy是另一种支持它的语言。基本上不是返回1true)或0false),而是根据参数是相等还是不相等,太空船运算符将返回10−1,具体取决于左参数相对于右参数的值。

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

它对排序数组很有用。

答案 1 :(得分:64)

当您在自己的类中定义并包含Comparable module时,太空船方法很有用。然后,您的班级将免费获得>, < , >=, <=, ==, and between?方法。

class Card
  include Comparable
  attr_reader :value

  def initialize(value)
    @value = value
  end

  def <=> (other) #1 if self>other; 0 if self==other; -1 if self<other
    self.value <=> other.value
  end

end

a = Card.new(7)
b = Card.new(10)
c = Card.new(8)

puts a > b # false
puts c.between?(a,b) # true

# Array#sort uses <=> :
p [a,b,c].sort # [#<Card:0x0000000242d298 @value=7>, #<Card:0x0000000242d248 @value=8>, #<Card:0x0000000242d270 @value=10>]

答案 2 :(得分:19)

这是一般的比较运算符。它返回-1,0或+1,具体取决于其接收器是否小于,等于或大于其参数。

答案 3 :(得分:15)

我将用简单的例子来解释

  1. [1,3,2] <=> [2,2,2]

    Ruby将从左侧开始比较两个数组的每个元素。 左数组的1小于右数组的2。因此左数组小于右数组。输出将为-1

  2. [2,3,2] <=> [2,2,2]

    如上所述,它将首先比较第一个相等的元素,然后比较第二个元素,在这种情况下,左数组的第二个元素更大,因此输出为1

答案 4 :(得分:1)

由于此运算符减少了与整数表达式的比较,因此它提供了基于多个列/属性对升序或降序进行排序的最通用方法。

例如,如果我有一个对象数组,我可以这样做:

# `sort!` modifies array in place, avoids duplicating if it's large...

# Sort by zip code, ascending
my_objects.sort! { |a, b| a.zip <=> b.zip }

# Sort by zip code, descending
my_objects.sort! { |a, b| b.zip <=> a.zip }
# ...same as...
my_objects.sort! { |a, b| -1 * (a.zip <=> b.zip) }

# Sort by last name, then first
my_objects.sort! { |a, b| 2 * (a.last <=> b.last) + (a.first <=> b.first) }

# Sort by zip, then age descending, then last name, then first
my_objects.sort! do |a, b|
      4 * (a.zip   <=> b.zip) +
     -3 * (a.age   <=> b.age) +
      2 * (a.last  <=> b.last) +
          (a.first <=> b.first)
end

这个基本模式可以推广到任意数量的列,按每个列的升序/降序排列。

答案 5 :(得分:-1)

  

什么是<=>(&#39;太空船&#39;运营商)

根据RFC that introduced the operator,$ a <=> $ b

 -  0 if $a == $b
 - -1 if $a < $b
 -  1 if $a > $b
 - Return 0 if values on either side are equal
 - Return 1 if value on the left is greater
 - Return -1 if the value on the right is greater

示例:

//Comparing Integers

echo 1 <=> 1; //ouputs 0
echo 3 <=> 4; //outputs -1
echo 4 <=> 3; //outputs 1

//String Comparison

echo "x" <=> "x"; // 0
echo "x" <=> "y"; //-1
echo "y" <=> "x"; //1

更多

// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1

// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1

echo "a" <=> "aa"; // -1
echo "zz" <=> "aa"; // 1

// Arrays
echo [] <=> []; // 0
echo [1, 2, 3] <=> [1, 2, 3]; // 0
echo [1, 2, 3] <=> []; // 1
echo [1, 2, 3] <=> [1, 2, 1]; // 1
echo [1, 2, 3] <=> [1, 2, 4]; // -1

// Objects
$a = (object) ["a" => "b"]; 
$b = (object) ["a" => "b"]; 
echo $a <=> $b; // 0