Constantize raises Uninitialized Constant Error

Viewed 5216

I'm working on this pretty thorny problem and decided I might be able to dynamically create a class that inherits from StandardError by doing this:

something = "JustForBelow"
error_class = "#{something}Error".constantize
error_class = StandardError.new

But I'm getting a really weird error (in my opinion), which is:

Uninitialized constant JustForBelowError

Aren't I initializing it right there?

(Essentially) same error comes up when I try this:

StandardError.const_get "#{something}Error"
# => NameError: uninitialized constant StandardError::JustForBelowClass

This feels really weird, because a) these are super random names; there aren't any conflicts, and b) I'm pretty sure I've used constantize like in the first example before. Any ideas what's going wrong?

3 Answers
Related