伙计们
我是ruby的初学者,在我的实践中,我想到了一个音乐脚本,有一点让我昏昏欲睡:当我在中输入Scale.major_by_note ('C')的时候,一切都很好,但是如果我输入Scale.major_by_note ('C #'),它就不能工作,为了让它工作,我必须放一个"C #/ Db",帮助我确保"C“和"C #”和"C #/ Db",谢谢!以下是脚本:
class Scale
NATURAL = %w[C D E F G A B].freeze
ACCIDENT = %w[C# Db D# Eb F# Gb G# Ab A# Bb].freeze
CHROMATIC = %w[C C#/Db D D#/Eb E F F#/Gb G G# A A#/Bb B].freeze
SCALE_MAJOR_PATTERN = [0, 2, 4, 5, 7, 9, 11, 12].freeze # T T st T T T st
SCALE_MINOR_PATTERN = [0, 2, 3, 5, 7, 8, 10, 12].freeze # T st T T st T T
def self.show_all_scales(note)
major = Scale.major_by_note(note)
minor = Scale.minor_by_note(note)
all = { major: major, minor: minor}
end
def self.major_by_note(note)
major_note_index = CHROMATIC.index(note)
SCALE_MAJOR_PATTERN.map do |major_interval| # Interação
major_scale_note_index = major_note_index + major_interval
if major_scale_note_index <= (CHROMATIC.length - 1)
CHROMATIC[major_scale_note_index]
else
reseted_major_scale_note_index = major_scale_note_index - CHROMATIC.length
CHROMATIC[reseted_major_scale_note_index]
end
end
end
def self.minor_by_note(note)
minor_note_index = CHROMATIC.index(note)
SCALE_MINOR_PATTERN.map do |minor_interval|
minor_scale_note_index = minor_note_index + minor_interval
if minor_scale_note_index <= (CHROMATIC.length - 1)
CHROMATIC[minor_scale_note_index]
else
reseted_minor_scale_note_index = minor_scale_note_index - CHROMATIC.length
CHROMATIC[reseted_minor_scale_note_index]
end
end
end
end```发布于 2021-04-20 03:11:30
当你输入的时候
%w[C C#/Db D D#/Eb E F F#/Gb G G# A A#/Bb B]Ruby正在将其转换为字符串数组:
["C", "C#/Db", "D", "D#/Eb", "E", "F", "F#/Gb", "G", "G#", "A", "A#/Bb", "B"]虽然你知道C#和Db是同一个音符,但Ruby并非如此,它认为本例中的音符叫做C#/Db。当它试图查找CHROMATIC.index("C#")时,它返回的是nil,因为数组中没有C#。
一种解决方案可能是这样编写它:
CHROMATIC = %w[C C# D D# E F F# G G# A A# B].freeze
CHROMATIC_PAIR_MAP = {
"Db" => "C#",
"Eb" => "D#",
"Gb" => "F#",
"Ab" => "B#",
}
...
def self.index_of_note(note)
CHROMATIC.index(note) ||
CHROMATIC.index(CHROMATIC_PAIR_MAP[note])
end
def self.major_by_note(note)
major_note_index = index_of_note(note)在这里,我创建了一个新的辅助方法来获取注释的索引,方法是直接从CHROMATIC数组中获取索引,或者在CHROMATIC_PAR_MAP散列中查找note键。只有当CHROMATIC.index(note)返回nil时,它才会在哈希表中执行查找。这是我在控制台(Irb)中得到的:
irb(main):191:0> Scale.major_by_note("C#")
=> ["C#", "D#", "F", "F#", "G#", "A#", "C", "C#"]
irb(main):192:0> Scale.major_by_note("Db")
=> ["C#", "D#", "F", "F#", "G#", "A#", "C", "C#"]
irb(main):193:0> Scale.major_by_note("D#")=> ["D#", "F", "G", "G#", "A#", "C", "D", "D#"]
irb(main):194:0> Scale.major_by_note("Eb")=> ["D#", "F", "G", "G#", "A#", "C", "D", "D#"]完整的新类:
class Scale
NATURAL = %w[C D E F G A B].freeze
ACCIDENT = %w[C# Db D# Eb F# Gb G# Ab A# Bb].freeze
CHROMATIC = %w[C C# D D# E F F# G G# A A# B].freeze
SCALE_MAJOR_PATTERN = [0, 2, 4, 5, 7, 9, 11, 12].freeze # T T st T T T st
SCALE_MINOR_PATTERN = [0, 2, 3, 5, 7, 8, 10, 12].freeze # T st T T st T T
CHROMATIC_PAIR_MAP = {
"Db" => "C#",
"Eb" => "D#",
"Gb" => "F#",
"Ab" => "B#",
}
def self.show_all_scales(note)
major = Scale.major_by_note(note)
minor = Scale.minor_by_note(note)
all = { major: major, minor: minor}
end
def self.major_by_note(note)
major_note_index = index_of_note(note)
SCALE_MAJOR_PATTERN.map do |major_interval| # Interação
major_scale_note_index = major_note_index + major_interval
if major_scale_note_index <= (CHROMATIC.length - 1)
CHROMATIC[major_scale_note_index]
else
reseted_major_scale_note_index = major_scale_note_index - CHROMATIC.length
CHROMATIC[reseted_major_scale_note_index]
end
end
end
def self.minor_by_note(note)
minor_note_index = CHROMATIC.index(note)
SCALE_MINOR_PATTERN.map do |minor_interval|
minor_scale_note_index = minor_note_index + minor_interval
if minor_scale_note_index <= (CHROMATIC.length - 1)
CHROMATIC[minor_scale_note_index]
else
reseted_minor_scale_note_index = minor_scale_note_index - CHROMATIC.length
CHROMATIC[reseted_minor_scale_note_index]
end
end
end
def self.index_of_note(note)
CHROMATIC.index(note) ||
CHROMATIC.index(CHROMATIC_PAIR_MAP[note])
end
endhttps://stackoverflow.com/questions/67166842
复制相似问题