module RDoc::RI::Paths

def self.gemdirs filter = :latest

def self.gemdirs filter = :latest
  ri_paths = {}
  all = Gem::Specification.map do |spec|
    [File.join(spec.doc_dir, 'ri'), spec.name, spec.version]
  end
  if filter == :all then
    gemdirs = []
    all.group_by do |_, name, _|
      name
    end.sort_by do |group, _|
      group
    end.map do |group, items|
      items.sort_by do |_, _, version|
        version
      end.reverse_each do |dir,|
        gemdirs << dir
      end
    end
    return gemdirs
  end
  all.each do |dir, name, ver|
    next unless File.exist? dir
    if ri_paths[name].nil? or ver > ri_paths[name].first then
      ri_paths[name] = [ver, name, dir]
    end
  end
  ri_paths.sort_by { |_, (_, name, _)| name }.map { |k, v| v.last }
rescue LoadError
  []
end