I am working on a class that represents musical pitches. I had two methods, spell_as_sharp
and spell_as_flat
that were completely identical except for exactly two characters. One of those characters was just a string, but the other represented a method. spell_as_sharp
subtracted one from a given number, where spell_as_flat
added one. I did this…
Before:
def spell_as_sharp
num = self.pitch_number
if VALID_PITCHES.has_value? num
VALID_PITCHES.key(num)
else
"#{VALID_PITCHES.key(ensure_number_scale(num - 1))}#"
end
end
def spell_as_flat
num = self.pitch_number
if VALID_PITCHES.has_value? num
VALID_PITCHES.key(num)
else
"#{VALID_PITCHES.key(ensure_number_scale(num + 1))}b"
end
end
And after:
def spell_as_sharp
respell :-, "#"
end
def spell_as_flat
respell :+, "b"
end
def respell method, accidental
num = self.pitch_number
if VALID_PITCHES.has_value? num
VALID_PITCHES.key(num)
else
VALID_PITCHES.key(ensure_number_scale(num.send(method, 1))) + accidental
end
end
Is this a good idea, or have I been writing too much scheme recently? Is it idiomatic?
Thanks
edit: The entire class is here.