Class: WTFChord::Note

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/wtf_chord/note.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, position = nil) ⇒ Note

Returns a new instance of Note.



7
8
9
10
11
12
13
14
15
16
# File 'lib/wtf_chord/note.rb', line 7

def initialize(key, position = nil)
  if /^(?<note>[a-h])(?<sign>#|b)?$/i =~ key
    note.upcase!
    @key      = "#{note}#{sign}".freeze
    @name     = "#{DIATONIC[note]}#{SIGNS[sign]}".freeze
    @position = position
  else
    raise ArgumentError
  end
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/wtf_chord/note.rb', line 5

def key
  @key
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/wtf_chord/note.rb', line 5

def name
  @name
end

#positionObject (readonly)

Returns the value of attribute position.



5
6
7
# File 'lib/wtf_chord/note.rb', line 5

def position
  @position
end

Instance Method Details

#<=>(other) ⇒ Object



34
35
36
37
38
39
# File 'lib/wtf_chord/note.rb', line 34

def <=> other
  case other
  when Note     then @position <=> other.position
  when Integer  then @position <=> other
  end
end

#==(other) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/wtf_chord/note.rb', line 26

def == other
  case other
  when String  then other.casecmp(@key).zero? || aliases.any? { |a| other.casecmp(a.key).zero? }
  when Integer then other == @position
  when Note    then other.position == @position
  end
end

#aliasesObject



41
42
43
# File 'lib/wtf_chord/note.rb', line 41

def aliases
  @aliases ||= SCALE.select { |t| t.position == @position && t.key != @key }
end

#chord(name) ⇒ Object



53
54
55
# File 'lib/wtf_chord/note.rb', line 53

def chord(name)
  with_octave.chord(name)
end

#inspectObject



22
23
24
# File 'lib/wtf_chord/note.rb', line 22

def inspect
  "#@name (#{to_s}: #@position)"
end

#semitone?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/wtf_chord/note.rb', line 45

def semitone?
  @key.end_with?(FLAT, SHARP)
end

#to_sObject



18
19
20
# File 'lib/wtf_chord/note.rb', line 18

def to_s
  @key.sub(/[b#]$/, SIGNS)
end

#with_octave(octave) ⇒ Object Also known as: []



49
50
51
# File 'lib/wtf_chord/note.rb', line 49

def with_octave(octave)
  Pitch.new(self, octave)
end