What is the relationship between the ruby ​​class and a module of the same name?

In my rails application, I have models / foo.rb and models / foo / exceptions.rb :

class Foo include Foo::Exceptions end module Foo::Exceptions class MySpecialException < Exception end end 

In the rails console, I see the following:

 >> Foo::MySpecialException Foo::Exceptions::MySpecialException < Exception >> Foo::MySpecialException == Foo::Exceptions::MySpecialException true >> Foo::MySpecialException === Foo::Exceptions::MySpecialException false 
  • What do you think of my file structure / namespace?
  • Do I need my inclusion string in Foo or does Rails automatically include these modules?
  • Why MySpecialException exist in the top-level namespace of Foo and point to Foo::Exceptions::MySpecialException ?
  • What does it mean that these two classes == , but not === ?

I researched this in irb, but continued to encounter errors that were incompatible with the behavior I (think) I saw in Rails.

related: What is the usual place to store custom exception definitions in a rails project?

+4
source share
2 answers

What is the relationship between the ruby ​​class and a module of the same name?

The class and module cannot have the same fully qualified name in ruby. It is possible to have a Foo::Bar class and a Baz::Bar module, in which case there is no connection between the class and the module. However, it is not possible to have the Foo::Bar class and the Foo::Bar module at the same time.

(I'm not sure if this is relevant to the rest of your question though)

Is my inclusion string in Foo necessary or does Rails automatically include these modules?

Rails does not automatically include your modules. However, this does not mean that you need to turn on yourself, you can simply access it. That is, use Exceptions::MySpecialException instead of MySpecialException inside the Foo class.

Why does a MySpecialException exist in the top-level Foo namespace and point to Foo :: Exceptions :: MySpecialException?

Because you included Foo::Exceptions in Foo . Because of this, all instance methods of Foo::Exceptions are also instances of the Foo method, and all Foo::Exceptions constants are also Foo constants - including MySpecialException .

What does it mean that these two classes ==, but not ===?

== means that it is the same class. The fact that this is not === means that the class is not an instance of itself (since x === y matches y.is_a?(x) if x is a class).

+3
source
  • I suggest you move the exceptions to lib: lib / exceptions / exception_name.rb
  • Rails will automatically load the material into dir models, however for the lib folder you must do this by setting config / application.rb and adding the following:

    # Custom directories with classes and modules you want to be autoloadable.

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

    config.autoload_paths += Dir["#{config.root}/lib/**/"]

+1
source

Source: https://habr.com/ru/post/1338665/


All Articles