Ruby Raise Exception Error
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Getting an error message when I use the following Ruby code:

class InvalidNameError < StandardError; end
def new_name(name, options={})
  {
    raise InvalidNameError.new if name.nil?
    name: name,
    year: options[:year],
    student: options[:student]
  }
end
begin
  game = new_name(nil)
rescue InvalidNameError => e
  puts "There was a problem creating your name: #{e.message}"
end
awarded to alex

Crowdsource coding tasks.

1 Solution

Winning solution

There we're two main things wrong with your script

  1. You had uneeded curly brackets {
  2. You should've used = instead of :

Your code should have looked like this:

class InvalidNameError < StandardError; end
def new_name(name, options={})
    raise InvalidNameError.new if name.nil?
    name = name,
    year = options[:year],
    student = options[:student]
end
begin
    game = new_name(nil)
rescue InvalidNameError => e
    puts "There was a problem creating your name: #{e.message}"
end
Thanks, this works now. I was having some confusing with putting the Raise before/after the {}
rsx 9 years ago
@rsx anytime!
alex 9 years ago