Enable the module with or without sending, is there a difference?

SomeClass.include(SomeModule) 

vs.

 SomeClass.send :include, SomeModule 

Are there any differences between the two? Why is the second option preferable? (At least I found this more often in other people's code).

+4
source share
2 answers

They are almost identical.

The difference is that if SomeClass#include is private, the latter can still call it due to the nature of Object#send

If SomeClass#include was private and you went first, this will throw an error.

Thus, the advantage of using the latter is that you can enable the module no matter what the visibility is. (Regardless of whether you really believe that access to private methods is the β€œright” thing to do, this is another story. It certainly gives you tremendous power.)

+5
source

You can reopen an existing class:

 class SomeClass include SomeModule end 
0
source

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


All Articles