rootulp's solution

to Scale Generator in the Ruby Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given a tonic, or starting note, and a set of intervals, generate the musical scale starting with the tonic and following the specified interval pattern.

Scales in Western music are based on the chromatic (12-note) scale.This scale can be expressed as the following group of pitches:

A, A#, B, C, C#, D, D#, E, F, F#, G, G#

A given sharp note (indicated by a #), can also be expressed as the flat of the note above it (indicated by a b), so the chromatic scale can also be written like this:

A, Bb, B, C, Db, D, Eb, E, F, Gb, G, Ab

The major and minor scale and modes are subsets of this twelve-pitch collection. They have seven pitches, and are called diatonic scales. The collection of notes in these scales is written with either sharps or flats, depending on the tonic. Here is a list of which are which:

No Accidentals: C major A minor

Use Sharps: G, D, A, E, B, F# major e, b, f#, c#, g#, d# minor

Use Flats: F, Bb, Eb, Ab, Db, Gb major d, g, c, f, bb, eb minor

The diatonic scales, and all other scales that derive from the chromatic scale, are built upon intervals. An interval is the space between two pitches.

The simplest interval is between two adjacent notes, and is called a "half step", or "minor second" (sometimes written as a lower-case "m"). The interval between two notes that have an interceding note is called a "whole step" or "major second" (written as an upper-case "M"). The diatonic scales are built using only these two intervals between adjacent notes.

Non-diatonic scales can contain the same letter twice, and can contain other intervals. Sometimes they may be smaller than usual (diminished, written "D"), or larger (augmented, written "A"). Intervals larger than an augmented second have other names.

Here is a table of pitches with the names of their interval distance from the tonic (A).

A A# B C C# D D# E F F# G G# A
Unison Min 2nd Maj 2nd Min 3rd Maj 3rd Per 4th Tritone Per 5th Min 6th Maj 6th Min 7th Maj 7th Octave
Dim 3rd Aug 2nd Dim 4th Aug 4th Dim 5th Aug 5th Dim 7th Aug 6th Dim 8ve
Dim 5th

For installation and learning resources, refer to the exercism help page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

``````gem install minitest
``````

If you would like color output, you can `require 'minitest/pride'` in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

``````ruby scale_generator_test.rb
``````

To include color from the command line:

``````ruby -r minitest/pride scale_generator_test.rb
``````

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

scale_generator_test.rb

``````require 'minitest/autorun'
require_relative 'scale_generator'

class ScaleGeneratorTest < Minitest::Test
def test_naming_scale
chromatic = Scale.new('c', :chromatic)
expected = 'C chromatic'
actual = chromatic.name
assert_equal expected, actual
end

def test_chromatic_scale
skip
chromatic = Scale.new('C', :chromatic)
expected = %w(C C# D D# E F F# G G# A A# B)
actual = chromatic.pitches
assert_equal expected, actual
end

def test_another_chromatic_scale
skip
chromatic = Scale.new('F', :chromatic)
expected = %w(F Gb G Ab A Bb B C Db D Eb E)
actual = chromatic.pitches
assert_equal expected, actual
end

def test_naming_major_scale
skip
major = Scale.new('G', :major, 'MMmMMMm')
expected = 'G major'
actual = major.name
assert_equal expected, actual
end

def test_major_scale
skip
major = Scale.new('C', :major, 'MMmMMMm')
expected = %w(C D E F G A B)
actual = major.pitches
assert_equal expected, actual
end

def test_another_major_scale
skip
major = Scale.new('G', :major, 'MMmMMMm')
expected = %w(G A B C D E F#)
actual = major.pitches
assert_equal expected, actual
end

def test_minor_scale
skip
minor = Scale.new('f#', :minor, 'MmMMmMM')
expected = %w(F# G# A B C# D E)
actual = minor.pitches
assert_equal expected, actual
end

def test_another_minor_scale
skip
minor = Scale.new('bb', :minor, 'MmMMmMM')
expected = %w(Bb C Db Eb F Gb Ab)
actual = minor.pitches
assert_equal expected, actual
end

def test_dorian_mode
skip
dorian = Scale.new('d', :dorian, 'MmMMMmM')
expected = %w(D E F G A B C)
actual = dorian.pitches
assert_equal expected, actual
end

def test_mixolydian_mode
skip
mixolydian = Scale.new('Eb', :mixolydian, 'MMmMMmM')
expected = %w(Eb F G Ab Bb C Db)
actual = mixolydian.pitches
assert_equal expected, actual
end

def test_lydian_mode
skip
lydian = Scale.new('a', :lydian, 'MMMmMMm')
expected = %w(A B C# D# E F# G#)
actual = lydian.pitches
assert_equal expected, actual
end

def test_phrygian_mode
skip
phrygian = Scale.new('e', :phrygian, 'mMMMmMM')
expected = %w(E F G A B C D)
actual = phrygian.pitches
assert_equal expected, actual
end

def test_locrian_mode
skip
locrian = Scale.new('g', :locrian, 'mMMmMMM')
expected = %w(G Ab Bb C Db Eb F)
actual = locrian.pitches
assert_equal expected, actual
end

def test_harmonic_minor
skip
harmonic_minor = Scale.new('d', :harmonic_minor, 'MmMMmAm')
expected = %w(D E F G A Bb Db)
actual = harmonic_minor.pitches
assert_equal expected, actual
end

def test_octatonic
skip
octatonic = Scale.new('C', :octatonic, 'MmMmMmMm')
expected = %w(C D D# F F# G# A B)
actual = octatonic.pitches
assert_equal expected, actual
end

def test_hexatonic
skip
hexatonic = Scale.new('Db', :hexatonic, 'MMMMMM')
expected = %w(Db Eb F G A B)
actual = hexatonic.pitches
assert_equal expected, actual
end

def test_pentatonic
skip
pentatonic = Scale.new('A', :pentatonic, 'MMAMA')
expected = %w(A B C# E F#)
actual = pentatonic.pitches
assert_equal expected, actual
end

def test_enigmatic
skip
enigmatic = Scale.new('G', :enigma, 'mAMMMmM')
expected = %w(G G# B C# D# F F#)
actual = enigmatic.pitches
assert_equal expected, actual
end

# Problems in exercism evolve over time, as we find better ways to ask
# questions.
# The version number refers to the version of the problem you solved,
#
# Define a constant named VERSION inside of the top level BookKeeping
# module, which may be placed near the end of your file.
#
# In your file, it will look like this:
#
# module BookKeeping
#   VERSION = 1 # Where the version number matches the one in the test.
# end
#
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

def test_bookkeeping
skip
assert_equal 1, BookKeeping::VERSION
end
end``````
``````# Scale
class Scale
ASCENDING_INTERVALS = %w(m M A).freeze
CHROMATIC_SCALE = %w(C C# D D# E F F# G G# A A# B).freeze
FLAT_CHROMATIC_SCALE = %w(C Db D Eb E F Gb G Ab A Bb B).freeze
FLAT_KEYS = %w(F Bb Eb Ab Db Gb d g c f bb eb).freeze

def initialize(tonic, scale_name, pattern = false)
@tonic = tonic
@scale_name = scale_name
@pattern = pattern
end

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

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

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

def chromatic_scale
FLAT_KEYS.include?(tonic) ? FLAT_CHROMATIC_SCALE : CHROMATIC_SCALE
end
end``````