# Custom error class for unsupported operations
class UnsupportedOperation < StandardError
def initialize(msg = "This operation is not supported.")
super(msg)
end
end
class SimpleCalculator
# Define the allowed operations
ALLOWED_OPERATIONS = ['+', '*', '/'].freeze
def self.calculate(first_operand, second_operand, operation)
# Validate input types for operands
unless first_operand.is_a?(Integer) && second_operand.is_a?(Integer)
raise ArgumentError.new("Invalid argument: operands must be integers.")
end
# Check if the operation is supported, otherwise raise an UnsupportedOperation error
unless ALLOWED_OPERATIONS.include?(operation)
raise UnsupportedOperation.new("Operation '#{operation}' is not supported.")
end
# Perform the operation
case operation
when "+"
"#{first_operand} + #{second_operand} = #{first_operand + second_operand}"
when "*"
"#{first_operand} * #{second_operand} = #{first_operand * second_operand}"
when "/"
# Handle division by zero
if second_operand == 0
return "Division by zero is not allowed."
else
"#{first_operand} / #{second_operand} = #{first_operand / second_operand}"
end
end
end
end
This is the error I get
NameError: uninitialized constant SimpleCalculator::UnsupportedOperation
Traceback (most recent call first):
Line 30:in `test_raises_exception_for_non_valid_operations'
Can someone explain why?