Jon Aquino's Mental Garden

Engineering beautiful software jon aquino labs | personal blog

Sunday, January 30, 2005

A slightly more complicated Ruby script...

... to facilitate the prioritizing of various items. It asks you to choose which of two values has the higher priority. Once it has asked you about all possible pairs, it prints a report of the items sorted by number of "hits":


class Item
attr_reader :tally, :description
def initialize(description)
@description = description
@tally = 0
end
def incx
@tally += 1
end
def <=>(other)
other.tally <=> @tally
end
end
items = [
"Compiling, Searching, or Researching",
"Imagining, Inventing, Creating, or Designing New Ideas",
"Analyzing, Breaking Down into its Parts",
"Adapting, Translating (incl. Computer Programming), Developing, or Improving",
"Visualing, Drawing, Painting, Dramatizing, Creating Videos or Software",
"Synthesizing, Combining Parts into a Whole",
"Deciding, Evaluating, Appraising, or Making Recommendations",
"Enabling Other People to Find or Retrieve Information",
"Communicating Well to Individuals in Writing (e.g. excellent letters)",
"Communicating Effectively to a Group or Multitude by Using Words Expressively in Speaking or Writing",
"In a Group: Leading, Taking the Lead, Being a Pioneer"
].collect{|x|Item.new(x)}
class Comparison
attr_reader :a, :b
def initialize(a, b)
if rand > 0.5 then
@a = a
@b = b
else
@a = b
@b = a
end
end
end
n = items.size
comparisons = []
class Array
def my_shuffle!
size .downto 1 do |n| push(delete_at(rand(n))) end
self
end
end
1.upto(n-1) {|i|
(i+1).upto(n) {|j|
comparisons << Comparison.new(items[i-1], items[j-1])
}
}
comparisons.my_shuffle!
def read_char
require "Win32API"
Win32API.new("crtdll", "_getch", [], "L").Call
end
def input(comparison)
case c = read_char.chr
when "q"
throw :quit
when "a"
comparison.a.incx
when "b"
comparison.b.incx
else
puts "Unrecognized command: " + c
input(comparison)
end
end
catch(:quit) do
comparisons.each {|comparison|
puts "a) " + comparison.a.description
puts "b) " + comparison.b.description
input(comparison)
}
end
items.sort.each {|item|
puts "#{item.tally} #{item.description}"
}

0 Comments:

Post a Comment

<< Home