Sha256: 5fd86da2b71a7d6095a490ef63de794e52b9ba5c36648a502ddee32a3e5a9f93

Contents?: true

Size: 1.06 KB

Versions: 396

Compression:

Stored size: 1.06 KB

Contents

class Scale
  ASCENDING_INTERVALS = %w(m M A)
  CHROMATIC_SCALE = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
  FLAT_CHROMATIC_SCALE = %w(C Db D Eb E F Gb G Ab A Bb B)
  FLAT_KEYS = %w(F Bb Eb Ab Db Gb d g c f bb eb)

  def initialize(tonic, scale_name, pattern = nil)
    @tonic = tonic.capitalize
    @scale_name = scale_name
    @pattern = pattern
    @chromatic_scale = FLAT_KEYS.include?(tonic) ? FLAT_CHROMATIC_SCALE : CHROMATIC_SCALE
  end

  def name
    "#{tonic} #{scale_name}"
  end

  def pitches
    return reorder_chromatic_scale unless pattern
    last_index = 0
    scale = pattern.each_char.with_object([]) do |c, collector|
      collector << reorder_chromatic_scale[last_index]
      last_index += ASCENDING_INTERVALS.index(c) + 1
    end
  end

  private

  attr_reader :tonic, :scale_name, :pattern, :chromatic_scale

  def reorder_chromatic_scale
    return chromatic_scale if tonic == 'C'
    index = chromatic_scale.index(tonic)
    chromatic_scale[index..-1] + chromatic_scale[0..index - 1]
  end
end

module BookKeeping
  VERSION = 1
end

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.179 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.178 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.177 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.176 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.175 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.174 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.173 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.172 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.171 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.170 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.169 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.167 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.166 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.165 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.164 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.163 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.162 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.161 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb
trackler-2.2.1.160 tracks/ruby/exercises/scale-generator/.meta/solutions/scale_generator.rb