Evan Hanson wrote all single >'d lines > On Wed, Mar 3, 2010 at 8:50 AM, Ben Rho <dearbenj / yahoo.com> wrote: >> picked up) > I'm not sure when "susb" would cause an issue -- do you mean an > instance like Gsusb? I don't know that that's a chord, but the G would > be read and the susb would raise an exception as an invalid chord > symbol. Yes, I do mean like Gsusb. Susb wasn't a valid example, but if it was actually the name of a type of chord, it would be counted as Gbsusb. Your code (with my comments): note[1..-1].scan(/./).each do |n| #for every character in the input except the first store it in n and do the following ( btw an easier way is simply note[1..-1].each('') ): if n == 'b' then flat! #if n is b then flat the base note elsif n == '#' then sharp! #else, if n is # then sharp the base note else raise ArgumentError, 'Invalid note name!' end #otherwise rase an error end #endfor For Gbsusb (assuming susb is a valid chord type) get the second character, which is b, and if it is b (which it is), flat the base note. Then do the same for s, then u, then s, then b (and flat it again), etc. On second thought, the sus should hit the ArgumentError, hm.. Can anyone explain that? >> Chord#to_s: >> Returns @name >> But that would only return the input in a fancy way! I don't see how it >> returns chords. > > It actually returns the individual notes in the chord, as generated by > Chord#names. #...# @name = note[0,1] #if note="Hello World" this sets @name to 'H' #...# #apply sharps and flats to @name #...# def to_s @name end #...# To me it seems that the base note is stored in @name, which is what is returned in to_s - so it would just return the base note. > As an aside, can anyone tell me if there is a slick Ruby way to do > what is done in cases like my Key#names, Chord#names, Chord#to_s, etc. > functions, where you're just mapping things from one array to another, > or from one array to a string, etc? See Jesús' answer. One thing to add - array * str is the same as array.join(str): [1,2,3]* #=> ERROR [1,2,3]*'' #=> '123' [1,2,3]*' ' #=> '1 2 3' [1,2,3].join #=> '123' [1,2,3].join(' ') #=> '1 2 3' Wait, I just realized some of my arguments are invalid! For some reason I've been looking at the Note class all along, instead of the Chord class! *facepalm* Sorry about that.. -------------------------- I started re-doing this Quiz from scratch about an hour ago without looking at anyone else's code, unfortunately, the results look about the same as David Springer's code (mine follows). > # Sharps the note in place... Comments are fun! I agree! They're also useful for anyone trying to read or debug your code! :) #I'll be making this library bigger, given enough time. I'd copy David Springer's (he has given me his permission), but this is case insensitive and doesn't have optional notes). chord_library={ '7' => [3,4,3], 'major' => [4,3], 'maj' => [4,3], 'major7' => [4,3,4], 'maj7' => [4,3,4], 'minor' => [3,4], 'min' => [3,4], 'minor7' => [3,4,3], 'min7' => [3,4,3], 'm7' => [3,4,3], 'sus' => [7], 'sus2' => [2,5], 'sus4' => [5,2] } rotated_notes = [] while !((chord=gets.to_s.chomp.downcase).empty?) base_note = chord[/^[abdeg]b/] if (base_note == nil) base_note = chord[/^[acdfg]#/] if (base_note == nil) base_note = chord[/^[a-g]/] if (base_note == nil) puts 'ERROR: Invalid base note' next end end end note_list = ((base_note[1,1]=='b') ? (%w(ab a bb b c db d eb e f gb g)) : (%w(a a# b c c# d d# e f f# g g#))) i=-1 rotated_notes = note_list.map do i += 1 note_list[(i + note_list.index(base_note))%12] end variation = ($'.empty?) ? ('major') : ($') if (chord_library[variation] == nil) puts 'ERROR: Chord not found' next end out = 'The notes in the chord ' + chord.capitalize + ' are: ' + base_note.capitalize note_tally = 0 chord_library[variation].each do |note| if (note<0) next end out << ' ' + rotated_notes[((note+note_tally)%12)].capitalize note_tally += note end puts out end -- Posted via http://www.ruby-forum.com/.