From: daz Date: 2003-09-28T17:31:23+09:00 Subject: Re: Thoughts on yield "Jim Freeze" wrote: > Nolan J. Darilek wrote: > > > > Have you considered parsing a string ... > For example, do: > > chord %{ c e g } > Like Jim, my feeling is that some parsing will be required. There's no C# method in Ruby. Allows for sharps, flats and naturals. My effort was also helped by Joel's input. Unique Note objects are created for no reason other than folks (and my) amusement. #==================== module Music class Clef def initialize @notes = [] end def time_sig(beats, beatval) @beats = beats @beatval = beatval end def chord(c) @notes << c.split.map {|ce| Note[ce] } end def notes(n=nil) if n @notes << n.split.map {|ne| Note[ne] } else @notes end end def inspect @n end end class Note private_class_method :new @@note_h = Hash[] def Note.[](n) puts "Bum Note - #{n}" if n !~ /[a-g][#_@]?/i nn = @@note_h[n] || @@note_h[n] = new(n) end def initialize(n) @n = n end def inspect @n end end def clef(&b) clf = Clef.new clf.instance_eval(&b) r = clf.notes p r r end end #==================== include Music clef do time_sig(4,4) notes %{d e f# g a b c# d} chord %{c d g} end clef do time_sig(3,4) notes %{a_ b@ c# d} chord %{d k} end #==================== #-> [[d, e, f#, g, a, b, c#, d], [c, d, g]] #-> Bum Note - k #-> [[a_, b@, c#, d], [d, k]] daz