Class: Clef::Note

Inherits:
Object
  • Object
show all
Defined in:
lib/clef/note.rb

Direct Known Subclasses

Rest

Constant Summary collapse

NOTES =
%w(C- C# D- D# E- F- F# G- G# A- A# B-)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, accent, octave) ⇒ Note

Returns a new instance of Note.



9
10
11
# File 'lib/clef/note.rb', line 9

def initialize(key, accent, octave)
  @key, @accent, @octave = key, accent, octave
end

Instance Attribute Details

#octaveObject (readonly)

Returns the value of attribute octave.



7
8
9
# File 'lib/clef/note.rb', line 7

def octave
  @octave
end

Class Method Details

.from_i(integer) ⇒ Object



25
26
27
28
29
# File 'lib/clef/note.rb', line 25

def self.from_i(integer)
  key = NOTES[integer % 12]
  octave = (integer / 12).to_i
  new(key[0..0], key[1..1], octave)
end

Instance Method Details

#+(rhs) ⇒ Object



17
18
19
# File 'lib/clef/note.rb', line 17

def +(rhs)
  Clef::Note.from_i(to_i + rhs)
end

#-(rhs) ⇒ Object



21
22
23
# File 'lib/clef/note.rb', line 21

def -(rhs)
  Clef::Note.from_i(to_i - rhs)
end

#<=>(rhs) ⇒ Object



39
40
41
# File 'lib/clef/note.rb', line 39

def <=>(rhs)
  self.to_i <=> rhs.to_i
end

#==(rhs) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/clef/note.rb', line 43

def ==(rhs)
  if rhs === Note
    to_s == rhs.to_s
  elsif rhs.is_a?(String)
    to_s == rhs
  end
end

#sharp?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/clef/note.rb', line 35

def sharp?
  @accent == '#'
end

#to_iObject



31
32
33
# File 'lib/clef/note.rb', line 31

def to_i
  NOTES.index("#{@key.upcase}#{@accent}") + (octave * 12)
end

#to_sObject



13
14
15
# File 'lib/clef/note.rb', line 13

def to_s
  "#{@key}#{@accent}#{@octave}"
end