From: Anthony Moralez Date: 2006-05-15T23:07:29+09:00 Subject: Re: [QUIZ] Tab Player (#79) Here is my attempt at a guitarist. class Guitarist def initialize(guitar=nil) @guitar = guitar unless guitar.nil? @guitar = Guitar.new(Guitar::CLEAN_ELECTRIC) if guitar.nil? end def play(tab) tab.chords.each { |notes| @guitar.play(notes) } @guitar.dump end end He's not a good guitarist. He only plays in the first 9 frets. He can play all of the included tabs on the included guitar. Of course the hard work is done by the tab reader. class Tab def initialize(tab_file) @chords = [] @file = tab_file @tab = extract_tabs(tab_file) end #select only lines containing tab notation and remove extraneous chars def extract_tabs(file) File.readlines(file).select { |line| line =~ /[eBCDAE|-][|-]/ }.collect { |line| line.gsub(/[eBGDAE|\s]/, '') } end def chords return [] if @tab.empty? return make_chords if @chords.empty? @chords end #break each string into individual notes # then zip and join the notes into the notation the guitar expects def make_chords @tab.collect! { |string| string.split(//) } (0...sections).each { |e| @chords << @tab[e+5].zip(@tab[e+4], @tab[e+3], @tab[e+2], @tab[e+1], @tab[e]).collect { |chord| chord.join } } @chords.flatten! end def sections @tab.length / 6 end end And that's all there is to my simple guitarist. Thanks Ross for another neat quiz. Anthony Moralez http://anthonysseblog.blogspot.com