From: Brian Candler on
Oops, the each_with_index was a left-over artefact. It should say:

def scale(pairs, degrees = [1,3,5,7])
degrees.collect do |d|
pair = pairs[(d-1) % pairs.size]
offset(pair[0], pair[1])
end
end
--
Posted via http://www.ruby-forum.com/.

From: Evan Hanson on
Thanks for the pointers. Maybe you could clarify some things below:

On Wed, Mar 3, 2010 at 8:50 AM, Ben Rho <dearbenj(a)yahoo.com> wrote:
>
> Also, could someone with some time give a brief run-through of what
> happens when Evan Hanson's code is run? I don't think I understand it.
> What I think happens:
> Make a new Chord object
> Chord#initialize:
>  Set @name to the note and @numval to to the notes position in
> Map.sharps
>  Parse note skipping the first character and sharp/flat it if it's b or
> # (I'd use a different method though, becouse susb or something would be
> 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.

>    Flat things by subtracting one from @numval (I think it should have
> an error check, @numval=11 if (@numval-=1)<0 to make it easier to port
> to other languages) and changing the value of @name (easier done in my
> opinion by using Map.flats[@numval])

Yes, the Map should probably loop. RIght now things like Cb are just
thrown out as invalid.

>    Sharp things by adding one from @numval (I think it should have an
> error check, @numval=0 if (@numval+=1)==12 to make it easier to port to
> other languages) and changing the value of @name (easier done in my
> opinion by using Map.sharps[@numval])

Same as above. I wrote the flat! and sharp! methods before I started
using the twelve-tone arrays, otherwise I might have done it that way.

> 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.

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?

From: Jesús Gabriel y Galán on
On Wed, Mar 3, 2010 at 8:13 PM, Evan Hanson <vnhnsn(a)gmail.com> wrote:

> 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?


> def names
> notes = []
> @notes.each { |n| notes.push n.name }
> notes
> end

def names
@notes.map {|n| n.name}
end

> def to_s
> out = ""
> names.each { |n| out += n + " " }
> out.strip!
> end

def to_s
names.join(" ")
end

Jesus.

From: Evan Hanson on
2010/3/3 Jesús Gabriel y Galán <jgabrielygalan(a)gmail.com>:
> On Wed, Mar 3, 2010 at 8:13 PM, Evan Hanson
>>  def names
>>    notes = []
>>    @notes.each { |n| notes.push n.name }
>>    notes
>>  end
>
> def names
>  @notes.map {|n| n.name}
> end
>
>>  def to_s
>>    out = ""
>>    names.each { |n| out += n + " " }
>>    out.strip!
>>  end
>
> def to_s
>  names.join(" ")
> end
>
> Jesus.
>

Ha, that's probably Ruby 101. Cool, thanks.

@Brian Candler -- I see you took the modal approach. Pretty cool. I like this:

(mode-1).times { a.push(a.shift) }

From: Ben Rho on
Evan Hanson wrote all single >'d lines
> On Wed, Mar 3, 2010 at 8:50 AM, Ben Rho <dearbenj(a)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/.

First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6
Prev: Random integer within a range?
Next: Please help!