Skip to content

Instantly share code, notes, and snippets.

@qizwiz
Created January 7, 2013 01:56
Show Gist options
  • Save qizwiz/4471676 to your computer and use it in GitHub Desktop.
Save qizwiz/4471676 to your computer and use it in GitHub Desktop.
# We want to make sure that each game is a valid game object - in this case a simple hash of values. Even still, we wouldn't want to return a hash with a nil name. Raise an InvalidGameError error in the new_game method if name is nil.
class InvalidGameError < StandardError; end
def new_game(name, options={})
{
name: name,
year: options[:year],
system: options[:system]
}
end
begin
game = new_game(nil)
rescue InvalidGameError => e
puts "There was a problem creating your new game: #{e.message}"
end
Copy link

ghost commented Nov 22, 2014

class InvalidGameError < StandardError; end
def new_game(name, options={})
 #add the following line. It will raise an error unless name is set
 raise InvalidGameError, "You must provide a name for this game." unless name
 {
    name: name,
    year: options[:year],
    system: options[:system]
  }
end
begin
  game = new_game(nil)
rescue InvalidGameError => e
  puts "There was a problem creating your new game: #{e.message}"
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment