Class: Upwords::LetterRack
- Inherits:
-
Object
- Object
- Upwords::LetterRack
- Defined in:
- lib/upwords/letter_rack.rb
Instance Attribute Summary collapse
-
#capacity ⇒ Object
readonly
Returns the value of attribute capacity.
-
#letters ⇒ Object
readonly
Returns the value of attribute letters.
Instance Method Summary collapse
- #add(letter) ⇒ Object
- #empty? ⇒ Boolean
- #full? ⇒ Boolean
- #has_letter?(letter) ⇒ Boolean
-
#initialize(capacity = 7) ⇒ LetterRack
constructor
A new instance of LetterRack.
- #remove(letter) ⇒ Object
- #show ⇒ Object
- #show_masked ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(capacity = 7) ⇒ LetterRack
Returns a new instance of LetterRack.
6 7 8 9 |
# File 'lib/upwords/letter_rack.rb', line 6 def initialize(capacity=7) @letters = [] @capacity = capacity end |
Instance Attribute Details
#capacity ⇒ Object (readonly)
Returns the value of attribute capacity.
4 5 6 |
# File 'lib/upwords/letter_rack.rb', line 4 def capacity @capacity end |
#letters ⇒ Object (readonly)
Returns the value of attribute letters.
4 5 6 |
# File 'lib/upwords/letter_rack.rb', line 4 def letters @letters end |
Instance Method Details
#add(letter) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/upwords/letter_rack.rb', line 27 def add(letter) if full? raise IllegalMove, "Rack is full!" else @letters << letter end end |
#empty? ⇒ Boolean
19 20 21 |
# File 'lib/upwords/letter_rack.rb', line 19 def empty? @letters.empty? end |
#full? ⇒ Boolean
15 16 17 |
# File 'lib/upwords/letter_rack.rb', line 15 def full? size == capacity end |
#has_letter?(letter) ⇒ Boolean
23 24 25 |
# File 'lib/upwords/letter_rack.rb', line 23 def has_letter?(letter) @letters.include? letter end |
#remove(letter) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/upwords/letter_rack.rb', line 35 def remove(letter) if has_letter?(letter) @letters.delete_at(@letters.index(letter)) else raise IllegalMove, "You don't have this letter!" end end |
#show ⇒ Object
43 44 45 |
# File 'lib/upwords/letter_rack.rb', line 43 def show @letters.join(' ') end |
#show_masked ⇒ Object
47 48 49 |
# File 'lib/upwords/letter_rack.rb', line 47 def show_masked @letters.map {'*'}.join(' ') end |
#size ⇒ Object
11 12 13 |
# File 'lib/upwords/letter_rack.rb', line 11 def size @letters.size end |