5

I've created a custom module (which currently only defines a new Exception class), and put it under lib/lib_th.rb

module LibTH
    module Error
        IDNotFound = Class.new
    end
end

I should not need to require/include the module in my code, as it should be automatically loaded, since it follows the conventional naming rules.

But when i try and raise the IDNotFound exception somewhere in my code:

res.size == 0 ? raise LibTH::Error::IDNotFound : res

I get the follwoing error:

SyntaxError (/Users/lrnz/code/ruby/corinna/app/models/treasure_hunt.rb:49: syntax error, unexpected tCONSTANT, expecting kDO or '{' or '('
  res.size == 0 ? raise LibTH::Error::IDNotFound : res
                             ^
/Users/lrnz/code/ruby/corinna/app/models/treasure_hunt.rb:49: syntax error, unexpected ':'
  res.size == 0 ? raise LibTH::Error::IDNotFound : res
                                                  ^):
app/controllers/treasure_hunts_controller.rb:50:in `show'

The strange thing is that I encounter no problems trying to raise the exception in script/console:

>> raise LibTH::Error::IDNotFound
LibTH::Error::IDNotFound: LibTH::Error::IDNotFound
from (irb):70

Thanks!

2 Answers 2

2

Nevermind, i solved the problem myself:

instead of using the if ? then : else statement, i expanded it into a:

raise LibTH::Error::IDNotFound if res.size == 0
res

It seems you can't use a constant value (as a class name) in the C-like if statement, thus the:

syntax error, unexpected tCONSTANT, expecting kDO or '{' or '('

Thanks asy!

Sign up to request clarification or add additional context in comments.

Comments

2

The constant isn't a problem, you just have a syntax error with the raise. If you changed it to:

res.size == 0 ? (raise LibTH::Error::IDNotFound) : res

It would work. The form you corrected to is better anyway, though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.