Ruby中的'<==>'是什么意思?
示例:代码来自以下比较格式中的数字的类x.x.x
,
def <==>(other)
# Some code here
end
以下代码来自此类订购数字,例如x.x.x
,
class Version
attr_reader :fst, :snd, :trd
def initialize(version="")
v = version.split(".")
@fst = v[0].to_i
@snd = v[1].to_i
@trd = v[2].to_i
end
def <=>(other)
return @fst <=> other.fst if ((@fst <=> other.fst) != 0)
return @snd <=> other.snd if ((@snd <=> other.snd) != 0)
return @trd <=> other.trd if ((@trd <=> other.trd) != 0)
end
def self.sort
self.sort!{|a,b| a <=> b}
end
def to_s
@sorted = @fst.to_s + "." + @snd.to_s + "." + @trd.to_s
#Puts out "#{@sorted}".
end
end