Avatar of w1zeman1p

w1zeman1p's solution

to Scale Generator in the Ruby Track

Published at Feb 21 2019 · 0 comments
Instructions
Test suite
Solution

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 Sharps or Flats: 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 other intervals. An "augmented first" interval, written "A", has two interceding notes (e.g., from A to C or Db to E). There are also smaller and larger intervals, but they will not figure into this exercise.


For installation and learning resources, refer to the Ruby resources 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
end
class Note
  attr_reader :name
  attr_reader :sharp_name
  attr_reader :flat_name

  def initialize(name)
    if name.length == 2
      left, right = name.chars
      @sharp_name = "#{left}#"
      @flat_name = "#{right}b"
    else
      @sharp_name = name
      @flat_name = name
    end
    @name = name
  end

  def half?
    @name.length == 2
  end

  def ==(other)
    if other.is_a?(String)
      other_name = other.downcase
      name.downcase == other_name ||
        sharp_name.downcase == other_name ||
        flat_name.downcase == other_name
    else
      super
    end
  end
end

class Scale
  NOTES = [
    Note.new("A"),
    Note.new("AB"),
    Note.new("B"),
    Note.new("C"),
    Note.new("CD"),
    Note.new("D"),
    Note.new("DE"),
    Note.new("E"),
    Note.new("F"),
    Note.new("FG"),
    Note.new("G"),
    Note.new("GA"),
  ]

  def initialize(tonic, scale, intervals='')
    @tonic = tonic
    @scale = scale
    @intervals = intervals.tr("mMA", "123").chars.map(&:to_i)
  end

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

  def pitches
    if use_flat?
      scale_notes.map(&:flat_name)
    elsif no_sharps_or_flats?
      scale_notes.map(&:name)
    else
      scale_notes.map(&:sharp_name)
    end
  end

  private

  attr_reader :tonic
  attr_reader :scale
  attr_reader :intervals

  def scale_notes
    index = NOTES.index(@tonic)
    notes = NOTES.rotate(index)
    if !chromatic?
      index = 0
      nts = [notes[index]]
      intervals.each do |skip|
        index += skip
        nts << notes[index]
      end
      notes = nts.compact
    end
    notes
  end

  def major?
    @scale == :major
  end

  def minor?
    @scale == :minor
  end

  def chromatic?
    @scale == :chromatic
  end

  def use_sharp?
    ['G', 'D', 'A', 'E', 'B', 'F#'].include?(@tonic) ||
      ['e', 'b', 'f#', 'c#', 'g#', 'd#'].include?(@tonic)
  end

  def use_flat?
    ['F', 'Bb', 'Eb', 'Ab', 'Db', 'Gb'].include?(@tonic) ||
      ['d', 'g', 'c', 'f', 'bb', 'eb'].include?(@tonic)
  end

  def no_sharps_or_flats?
    return true if @tonic == 'C' && major?
    return true if @tonic == 'a' && minor?
    false
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?