28 June 2007
andThe sort method in Ruby uses the boat operator, sometimes called ‘spaceship’, to compare object instances. In this episode Manfred shows how you can make your objects sortable by defining the boat operator.
28 June 2007
andThe sort method in Ruby uses the boat operator, sometimes called ‘spaceship’, to compare object instances. In this episode Manfred shows how you can make your objects sortable by defining the boat operator.
attr_accessor :tokens
@tokens = text.downcase.split(/ /)
end
attr_writer :priority
; @priority || 0; end
if other.kind_of?(Book)
if (important = (other.priority <=> priority)
) != 0
important
else
tokens <=> other.tokens
end
else
raise ArgumentError,
"Don't know how to compare a to a Book."
end
end
end