簡體   English   中英

什么是 Ruby <=>(宇宙飛船)運算符?

[英]What is the Ruby <=> (spaceship) operator?

什么是 Ruby <=>(宇宙飛船) <=>符? 運算符是否由任何其他語言實現?

spaceship 運算符將根據左側參數相對於右側參數的值返回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

它通常用於對數據進行排序。

它也被稱為三向比較運算符 Perl 可能是第一種使用它的語言。 其他一些支持它的語言是 Apache Groovy、PHP 7+ 和 C++20。

當您在自己的類中定義 spaceship 方法並包含Comparable 模塊時,該方法很有用。 然后,您的班級將獲得>, < , >=, <=, ==, 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>]

我會用簡單的例子來解釋

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

    Ruby 將從左側開始比較兩個數組的每個元素。 左側數組的1小於右側數組的2 因此左數組小於右數組。 輸出將是-1

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

    如上所述,它將首先比較相等的第一個元素,然后比較第二個元素,在這種情況下,左數組的第二個元素更大,因此輸出為1

這是一個通用的比較運算符。 它返回 -1、0 或 +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
# [Notice powers of 2 make it work for > 2 columns.]
my_objects.sort! do |a, b|
      8 * (a.zip   <=> b.zip) +
     -4 * (a.age   <=> b.age) +
      2 * (a.last  <=> b.last) +
          (a.first <=> b.first)
end

這種基本模式可以推廣為按任意數量的列排序,在每個列上以任何升序/降序排列。

<=>是什么(“宇宙飛船”運算符)

根據介紹操作符的 RFC , $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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM