我有一个名为的模块Votable,我用它来为不同的类提供votes属性vote_up和vote_down方法。但是,我还希望Votable对象集合按其票数排序。有没有办法可以使用这个模块来定义这种排序行为?
module Votable
  attr_reader :votes
  def votes
    @votes ||= 0
  end
  def vote_up
    @votes += 1
  end
  def vote_down
    @votes -= 1
  end
end
class Topic
  def initialize
    @comments = []
  end
  def add_comment(comment)
    @comments << comment
  end
  def comments
    # this code needs to be duplicated in every class that has a
    # collection of votables, but on a different collection
    @comments.sort { |a,b| b.votes <=> a.votes }
  end
end
class Comment
  include Votable
end