Class: WTFChord::Note
- Inherits:
-
Object
- Object
- WTFChord::Note
- Includes:
- Comparable
- Defined in:
- lib/wtf_chord/note.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#position ⇒ Object
readonly
Returns the value of attribute position.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #aliases ⇒ Object
- #chord(name) ⇒ Object
-
#initialize(key, position = nil) ⇒ Note
constructor
A new instance of Note.
- #inspect ⇒ Object
- #semitone? ⇒ Boolean
- #to_s ⇒ Object
- #with_octave(octave) ⇒ Object (also: #[])
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
#key ⇒ Object (readonly)
Returns the value of attribute key.
5 6 7 |
# File 'lib/wtf_chord/note.rb', line 5 def key @key end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/wtf_chord/note.rb', line 5 def name @name end |
#position ⇒ Object (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 |
#aliases ⇒ Object
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 |
#inspect ⇒ Object
22 23 24 |
# File 'lib/wtf_chord/note.rb', line 22 def inspect "#@name (#{to_s}: #@position)" end |
#semitone? ⇒ Boolean
45 46 47 |
# File 'lib/wtf_chord/note.rb', line 45 def semitone? @key.end_with?(FLAT, SHARP) end |
#to_s ⇒ Object
18 19 20 |
# File 'lib/wtf_chord/note.rb', line 18 def to_s @key.sub(/[b#]$/, SIGNS) end |