У меня есть следующий класс Linea
class Linea
include Comparable
attr_accessor :maxcap, :capatuale, :citta1, :citta2, :lineac
def initialize(c1, c2, maxc)
raise CapacitaIllegale if maxc <= 0
@maxcap = maxc
@capatuale = maxc
@citta1 = c1
@citta2 = c2
@lineac = []
@lineac.push(c1)
@lineac << c2
end
def <=> (l)
if(l.is_a?(Linea))
if (l.maxcap>@maxcap)
return l.maxcap
end
if(l.maxcap==@maxcap)
if (l.citta1.pop + l.citta2.pop>@citta1.pop+@citta2.pop)
return l.maxcap
end
end
end
return -1
end
end
после в классе Rete, куда я хочу вставить Linea и выполнить сортировку, используя <=> так;
class Rete
attr_accessor :lineec, :guasto_seg
def initialize()
@rete = []
@guasto_seg = []
end
def aggiungi_linea(c1, c2, maxc)
l = Linea.new(c1, c2, maxc)
unless (citta_presente(c1, c2))
@rete << l
@rete.sort { |l1,l2| l1<=>l2 if l1.is_a?(Linea) and l2.is_a?(Linea) }
else
raise LineaGiaEsistente
end
end
+ some diferent method
end
Iхотел спросить объяснения того, как он используется для сортировки, если мой прав, если <=> сделано и вызвано в сортировку так правильно. ???????Пожалуйста, дайте мне ответ как можно скорее, спасибо.