Class: Tetsujin::Theory::Notes

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/tetsujin/theory/notes.rb

Direct Known Subclasses

Scale

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notes:) ⇒ Notes

Returns a new instance of Notes.

Parameters:

Raises:

  • (TypeError)


12
13
14
15
16
# File 'lib/tetsujin/theory/notes.rb', line 12

def initialize(notes:)
  raise TypeError unless notes.is_a?(Array)
  raise ArgumentError unless notes.all? { |note| note.is_a?(Tetsujin::Theory::Note) }
  @notes = notes
end

Instance Attribute Details

#notesObject (readonly)

Returns the value of attribute notes.



9
10
11
# File 'lib/tetsujin/theory/notes.rb', line 9

def notes
  @notes
end

Instance Method Details

#+(other) ⇒ Tetsujin::Theory::Notes

Returns 2つの音の和集合.

Parameters:

Returns:



32
33
34
35
# File 'lib/tetsujin/theory/notes.rb', line 32

def +(other)
  new_notes = (notes + other.notes).uniq.sort
  Tetsujin::Theory::Notes.new(notes: new_notes)
end

#==(other) ⇒ Boolean

Returns 同じ音を持つかどうか.

Parameters:

Returns:

  • (Boolean)

    同じ音を持つかどうか



20
21
22
23
24
25
26
27
28
# File 'lib/tetsujin/theory/notes.rb', line 20

def ==(other)
  self_sorted = notes.sort do |a, b|
    [a.octave, a.pitch_class] <=> [b.octave, b.pitch_class]
  end
  other_sorted = other.notes.sort do |a, b|
    [a.octave, a.pitch_class] <=> [b.octave, b.pitch_class]
  end
  self_sorted == other_sorted
end