D
D
DartNyan2016-03-30 20:29:54
Ruby on Rails
DartNyan, 2016-03-30 20:29:54

When a module is included, is its code inlined as a macro or are methods simply called from it?

Hello.
I'm just starting to learn Ruby. (I haven’t gotten to RoR yet, but as I understand it, there will be no difference on this issue)
I read that many modules are divided into two parts. (ClassMethods and InstanceMethods)
And in this case, they are connected not as usual sub modules through "::", but through a callback
By the way, why? I did not understand this moment
And what's the point in submodules? Is this just for convenience, or is connecting a module directly embedding code from it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kungurov, 2016-03-31
@DartNyan

Allows you to shorten the code a little, adding elegance and immediately difficulty in understanding. If the module is divided into still nested modules, then thanks to the included, extended and inherited callbacks, it allows you to perform any actions with the heir class in accordance with their name (included / extend will be called when this module is nested in another module or class (include SomeModule, extend SomeModule and inherited when the interpreter reads the code from top to bottom, it will execute as the inheritance call SomeClass < OtherClass)
Thanks to these callbacks, Rails uses metaprogramming to extend the receiver class with additional functionality, saving us from writing this functionality explicitly in the class.
Details here: www .sitepoint.com/hitchhikers-guide-to-metaprogram...
Already further you can google yourself, which is not clear, he gave the direction.

A
Artem Pyankov, 2016-03-31
@ibub1ik

When a module is connected, it simply enters the method lookup path, no code transfer occurs.
Modules with ClassMethods/InstanceMethods submodules are included via a macro because it's more convenient (less code), and DRY is one of the Ruby idioms. No one forbids connecting manually, it's just that it's customary in the community to do this through included.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question