From: Shane Emmons Date: 2006-05-17T01:00:32+09:00 Subject: [SOLUTION] Tab Player (#79) Here is my simple solution. The main work horse of the whole thing is the Tab class. Which I will display first. --BEGIN CODE-- #!/usr/local/bin/ruby -w class Tab attr_reader :file, :music def initialize( file ) @file = file end def parse( file = @file ) @music = Array.new tab = Hash.new { |hash, key| hash[key] = Array.new } File.open(file).read.each do |line| next unless line =~ /^[EADGBe]/ bar = line.chomp.split(//) bar.each do |note| next if note =~ /[EADGBe|]/ tab[bar[0]] << note end end ['E', 'A', 'D', 'G', 'B', 'e'].each do |string| tab[string].each_index do |i| @music[i] = '' unless @music[i] @music[i] += tab[string][i] end end @music end end if __FILE__ == $0 tab = Tab.new('tabs/Em.tab') print "\"#{tab.parse.join('", "')}\"\n\n" end --END CODE-- Next we have the tabplayer program, which uses the Tab and Guitar classes to create some music. --BEGIN CODE-- #!/usr/local/bin/ruby -w require 'lib/guitar' require 'lib/tab' Tab.new(ARGV[0]).parse axe = Guitar.new Tab.new(ARGV[0]).parse.each do |notes| axe.play(notes) end print axe.dump --END CODE-- I have some test code that I used while developing the Tab class. --BEGIN CODE-- #!/usr/local/bin/ruby -w require 'test/unit' require 'lib/tab' class TC_Tab < Test::Unit::TestCase def setup test_construction end def test_construction @tab = Tab.new('tabs/Em.tab') assert_not_nil(@tab) assert_instance_of(Tab, @tab) assert_equal('tabs/Em.tab', @tab.file) assert_nil(@tab.music) end def test_read @tab.parse assert_equal( [ '------', '0-----', '------', '-2----', '------', '--2---', '------', '---0--', '------', '----0-', '------', '-----0' ], @tab.music ) @tab.parse('tabs/AmStrum.tab') assert_equal( [ '------', '-0----', '------', '--2---', '------', '---2--', '------', '----1-', '------', '-----0', '------', '------', '------', '-02210', '------', '------', '------', '------', '------', '------', '------', '------', '------', '------' ], @tab.music ) end end --END CODE-- Since I cannot attach a zip file from the Ruby Forum, I will just type the directory structure. If you would like a zipfile of the code along with the tabs, etc. just let me know. Thanks for an interesting quiz. tab_player(folder) | |-tabplayer.rb | |-lib | |-guitar.rb | |-tab.rb | |-tabs | |-*.tab files | |-test |-tc_tab.rb -- Posted via http://www.ruby-forum.com/.