Skip to content

Commit

Permalink
Alignment enforces validity of arguments
Browse files Browse the repository at this point in the history
Had to learn how to raise exceptions and how to expect them in rspec.
Learned about the syntax for writing a setter too, kinda cool.
Also, is it a good approach to define the valid list in the method like
I'm doing? Should that list be a constant somewhere so it doesn't have
to be created each time the method is called? I'm sure it should, but
what's the Ruby Way of doing that?
  • Loading branch information
Eric Coleman committed May 4, 2016
1 parent c946958 commit cc12332
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/character.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
class Character
attr_accessor :name
attr_accessor :alignment
attr_reader :alignment

def alignment=(alignment)
valid_alignments = ['Good', 'Evil', 'Neutral']
if not valid_alignments.include? alignment.capitalize
raise ArgumentError
end
@alignment = alignment.capitalize
end

end

4 changes: 4 additions & 0 deletions spec/character_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
expect(subject.alignment).to eq(expected_alignment)
end

it "raises ArgumentError when given an invalid alignment" do
expect{subject.alignment = "Decent"}.to raise_error(ArgumentError)
end

end


0 comments on commit cc12332

Please sign in to comment.