I’m working on a ruby program where I need to prompt the user to enter both their first and last names in a single line. The program should throw an exception if the user only provides one name (either first or last). I’m running into an error at the end of my code, code as shown below.
Error Code:
class MyNewException < Exception
attr_accessor :first, :last
def initialize (first, last)
@first = first
@last = last
end
end
print "Enter your first and last name:"
begin
first, last = gets.chomp.split
print "Hello," + first + " " + last + "!"
if last.size == 0
raise MyNewException, "Sorry, I didn't catch that! Try again:"
end
rescue MyNewException
puts "Sorry, I didn't catch that. Try again:"
retry
end
Keep getting an error in above code:
testing.rb:15:in `+': no implicit conversion of nil into String (TypeError)
The error occurs because the split
method may return fewer than two elements if the user enters only one name. In this case, last
will be nil
, and when attempting to concatenate it with a string, Ruby raises a TypeError
.
Here’s an explanation and a corrected version of the code:
Problem in the Original Code:
- When the user inputs only one name,
split
assigns that name tofirst
, andlast
becomesnil
. - Attempting to concatenate
nil
with a string using+
(in the lineprint "Hello," + first + " " + last + "!"
) causes theTypeError
becausenil
cannot be implicitly converted to a string.
Corrected Code:
To fix this, we can verify if last
is nil
or empty before attempting to concatenate. Here’s the corrected version:
codeclass MyNewException < Exception
attr_accessor :first, :last
def initialize(first, last)
@first = first
@last = last
end
end
print "Enter your first and last name:"
begin
first, last = gets.chomp.split
# Check if last name is missing
if last.nil? || last.empty?
raise MyNewException.new(first, last), "Sorry, I didn't catch that! Try again:"
end
print "Hello, " + first + " " + last + "!"
rescue MyNewException => e
puts e.message
retry
end
Explanation of the Changes:
- Check for
nil
or Emptylast
: Before concatenatingfirst
andlast
, we check iflast
isnil
or an empty string. If it is, we raise theMyNewException
. - Using
MyNewException.new(first, last)
: We create an instance ofMyNewException
withfirst
andlast
, ensuring the exception handling works as intended. - Improved Error Message Display: When the exception is raised,
e.message
provides a clearer error message to prompt the user to retry.
With these changes, the code will handle the situation where only one name is entered, avoiding the TypeError
.