Class: WTFChord::Fretboard

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

Direct Known Subclasses

Fingering

Constant Summary collapse

DEAD =
"×".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*strings) ⇒ Fretboard

Returns a new instance of Fretboard.



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

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.



7
8
9
# File 'lib/wtf_chord/fretboard.rb', line 7

def capo
  @capo
end

#stringsObject (readonly)

Returns the value of attribute strings.



7
8
9
# File 'lib/wtf_chord/fretboard.rb', line 7

def strings
  @strings
end

Instance Method Details

#[](idx) ⇒ Object



14
15
16
17
18
# File 'lib/wtf_chord/fretboard.rb', line 14

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

#fingersObject



34
35
36
# File 'lib/wtf_chord/fretboard.rb', line 34

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

#set_capo(capo = 0) ⇒ Object



20
21
22
23
24
# File 'lib/wtf_chord/fretboard.rb', line 20

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

#to_sObject Also known as: inspect



38
39
40
# File 'lib/wtf_chord/fretboard.rb', line 38

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

#with_capo(capo) ⇒ Object



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

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