Project

ranking

0.0
No commit activity in last 3 years
No release in over 3 years
Extends Set with a score. Sorts by score on #to_a, #each, etc
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.0
 Project Readme

ranking

Description

Extends Set to be ordered by a score. Sort of like a set, in that there are no duplicates, but also sort of like an array, in that it's ordered.

Create an instance, add some objects to it, and set the scores for those objects. When you iterate or slice the instance it'll be sorted by score behind the scenes.

Examples

require 'ranking'

ranking = Ranking.new
ranking << :foo
ranking << :bar

ranking.inc(:foo, 2)      # Incremente score of :foo by one
ranking.dec(:bar)         # Decrement, defaults to 1
ranking.score(:baz, 3)    # Set score explicitly, implicitly adds :baz to the set

ranking.to_a              # => [:baz, :foo, :bar]
ranking.scores[:foo]      # => 2

Or, if you wanted something more useful, here's a script I wrote to see which commands in my .bash_history are the most common:

require 'ranking'

word_freq = Ranking.new

File.open(File.expand_path("~/.bash_history")).each_line do |line|
  words = line.chomp.split(" ")
  words.each_index do |i|
    word_freq.inc( words[0..i].join(" ") )
  end
end

puts "Top 50 commands in ~/.bash_history"
puts "-------"
word_freq[0..49].each_index do |i|
  word = word_freq[i]
  puts "#{"%2i" % [i+1]}) #{word} (#{word_freq.scores[word]})"
end

Install

gem 'ranking'

Copyright

Copyright (c) 2012 Ryan Michael

See LICENSE.txt for details.