Help with simple calculator exercise (ruby)

# 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?

I edited your post for you to add codeblocks so it’s easier to read :)

1 Like

Did you initialize a SimpleCalculator::UnsupportedOperation value?

1 Like

As @IsaacG alludes, The tests expect a SimpleCalculator::UnsupportedOperation.

In your code I see an UnsupportedOperation class being created. but I don’t see a SimpleCalculator::UnsupportedOperation class being created :slight_smile:

1 Like

Oh wow I just needed to put the Unsupported < StandardError class in the Simple Calculator class. I was so confused why it wasnt working. Thank you for pointing me in the right direction!

2 Likes