Why do people use `Module.send (: prepend, ...)`?

I am learning how to use Module.prependinstead alias_method_chainin my Ruby code, and I noticed that some people use sendto call it ( example ):

ActionView::TemplateRenderer.send(:prepend,
    ActionViewTemplateRendererWithCurrentTemplate)

Others call it directly ( example ):

ActionView::TemplateRenderer.prepend(ActionViewTemplateRendererWithCurrentTemplate)

And, although I have not seen anyone use this style, I suspect that from the documentation you can even write this in the module from which you added:

module ActionViewTemplateRendererWithCurrentTemplate
    # Methods you're overriding go here

    prepend_features ActionView::TemplateRenderer
end

Is there a difference between these three styles? Is there any reason to approve one of the others?

+4
source share
1 answer

Module#prepend Ruby 2.0.0.

private, :

module Foo
  # ...
end

class Bar
  prepend Foo

  # ... The rest of the class definition ...
end

, - ( ). , :

Bar.send(:prepend, Foo)

Ruby version 2.1.0 Module#prepend - :

Bar.prepend(Foo)

, , Ruby 2.0.0 ( 24 2016), , , .send(:prepend, ...).

Module#include ( Ruby ) <= 2.0.0 2.1.0.

+6

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


All Articles