From: Daniel Moore Date: 2009-03-08T04:52:47+09:00 Subject: [SUMMARY] Polyrhythm Trainer (#194) This quiz may be worthy of a revisit in the future. Until then, here is my solution: the beginnings of an "Annoyance" trainer. # rhythm.rb class Rhythm BPM = 20 SECONDS_PER_MINUTE = 60.0 def initialize(n, sym) @sym = sym @seconds_per_hit = SECONDS_PER_MINUTE/(BPM*n) @last_hit = @seconds_per_hit @hit_count = 0 @time = 0 puts @seconds_per_hit end def elapse(time_elapsed) hit = false if @last_hit >= @seconds_per_hit @hit_count += 1 @last_hit -= @seconds_per_hit puts "#{"%1.3f" % @time}: #{@sym}" hit = true end @time += time_elapsed @last_hit += time_elapsed return hit end end s = '@' rhythms = ARGV.map{|arg| Rhythm.new(arg.to_i, s = s.succ)} delta = 0.05 time = 0 while true hit = rhythms.map{|r| r.elapse(delta)}.inject(false) {|hit, r| hit || r} print "\a" if hit time += delta sleep delta end The program will beep indefinitely, producing a sound every time the rhythm should be struck (to the nearest 20th of a second). Usage: ruby rhythm.rb [rhythms] EX: > ruby rhythm.rb 3 2 1.0 1.5 0.000: A 0.000: B 1.000: A 1.500: B 2.000: A 3.000: A 3.000: B 4.000: A 4.500: B 5.000: A Crank up the volume on your PC speaker and have a blast near your co-workers! http://rubyquiz.strd6.com/quizzes/194