Class: WTFChord::Fretboard

Inherits:
Object
  • Object
show all
Defined in:
lib/wtf_chord/fretboard.rb

Direct Known Subclasses

Fingering

Constant Summary collapse

DEAD =
"×"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*strings) ⇒ Fretboard

Returns a new instance of Fretboard.



11
12
13
14
# File 'lib/wtf_chord/fretboard.rb', line 11

def initialize(*strings)
  @capo    = 0
  @strings = strings.map { |string| GuitarString.new(string, @capo) }
end

Instance Attribute Details

#capoObject (readonly)

Returns the value of attribute capo.



9
10
11
# File 'lib/wtf_chord/fretboard.rb', line 9

def capo
  @capo
end

#stringsObject (readonly)

Returns the value of attribute strings.



9
10
11
# File 'lib/wtf_chord/fretboard.rb', line 9

def strings
  @strings
end

Instance Method Details

#[](idx) ⇒ Object



16
17
18
19
20
# File 'lib/wtf_chord/fretboard.rb', line 16

def [] idx
  case idx
  when 1..@strings.length then @strings[-idx]
  end
end

#fingersObject



36
37
38
# File 'lib/wtf_chord/fretboard.rb', line 36

def fingers
  @strings.map { |string| string.dead? ? DEAD : string.fret  }
end

#set_capo(capo = 0) ⇒ Object



22
23
24
25
26
# File 'lib/wtf_chord/fretboard.rb', line 22

def set_capo(capo = 0)
  @capo = capo
  @strings.each { |string| string.set_capo(@capo) }
  self
end

#to_sObject Also known as: inspect



40
41
42
# File 'lib/wtf_chord/fretboard.rb', line 40

def to_s
  "[ #{fingers * " "} ]"
end

#with_capo(capo) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/wtf_chord/fretboard.rb', line 28

def with_capo(capo)
  capo_was = @capo
  set_capo(capo)
  yield
ensure
  set_capo(capo_was)
end