Unified constant when using custom exceptions

I am trying to create custom exceptions for my application. I have a sample library in the libs folder with the following folder structure:

- lib/
|
|--social/
  |
  |-- bandcamp.rb

And this content is bandcamp.rbas follows:

module Social
  class ExampleException < Exception; end

  class Bandcamp
    def some_method()
    end
  end
end

And the whole point is that I can use Social::Bandcamp.new.some_methodanywhere in my application, and it works fine, but I can’t access it Social::ExampleExceptionand never raise it anywhere. It gives me

NameError: uninitialized constant Social::ExampleException

Do you have any ideas what I can do wrong? I am completely new to creating my own libraries, so I’m sure that something did not understand.

+4
source share
2 answers

, lib/ (config.autoload_paths). Autoload , . bandcamp.rb Social::Bandcamp, .

, .

+3

, Rails. ? :

2.0.0p353 :001 > Social.constants
 => [] 
2.0.0p353 :002 > raise Social::ExampleException
NameError: uninitialized constant Social::ExampleException
2.0.0p353 :003 > Social::Bandcamp
 => Social::Bandcamp 
2.0.0p353 :004 > Social.constants
 => [:ExampleException, :Bandcamp] 
2.0.0p353 :005 > raise Social::ExampleException
Social::ExampleException: Social::ExampleException

T , lib ( app), environment.rb ( ):

  config.eager_load_paths += %W(#{config.root}/lib)
+4

All Articles