Extending/includings

Hi
Thanks for these many helpful answers!
@Josh: what are TL;DR - Objects?
Didnt find any meaningful with google
Berg

···

Am 27.01.2016 14:08 schrieb "Joshua Bodah" <jb3689@yahoo.com>:

Module::extend, Module::include, and Module::prepend are all methods that
modify the ancestor chain at runtime. They each change the inheritance
chain.

Before we compare them, you should also be familiar with the concept of
singleton classes. The short of it is that every instance of Object has a
singleton class. A singleton class is an anonymous class instance that only
that object inherits from, and the singleton class is always considered to
be the closest ancestor of the object. It's often used to give particular
Object instances specific behavior.

TL;DR - Objects have singleton classes, ancestor chain is used for method
resolution; whoever is the closest ancestor to implement a method is the
one whose implementation of that method will get called (just like
inheritance in classical languages)

Back to the Module methods, Module::include inserts an ancestor in the
Module's ancestor chain immediately after the module itself:

irb(main):001:0> module MyModule; end
=> nil
irb(main):002:0> Array.ancestors
=> [Array, Enumerable, Object, Kernel, BasicObject]
irb(main):003:0> Array.include MyModule
=> Array
irb(main):004:0> Array.ancestors
=> [Array, MyModule, Enumerable, Object, Kernel, BasicObject]
Module::prepend actually puts the given module *before* the module iteself:

irb(main):001:0> module MyModule; end
=> nil
irb(main):002:0> Array.ancestors
=> [Array, Enumerable, Object, Kernel, BasicObject]
irb(main):003:0> Array.prepend MyModule
=> Array
irb(main):004:0> Array.ancestors
=> [MyModule, Array, Enumerable, Object, Kernel, BasicObject]

This means methods defined in MyModule will get called even before methods
in Array

Lastly is ::extend which works with the singleton class:

irb(main):001:0> module MyModule; end
=> nil
irb(main):002:0> Array.extend MyModule
=> Array
irb(main):003:0> Array.ancestors
=> [Array, Enumerable, Object, Kernel, BasicObject]
irb(main):004:0> Array.singleton_class.ancestors
=> [#<Class:Array>, MyModule, #<Class:Object>, #<Class:BasicObject>,
Class, Module, Object, Kernel, BasicObject]

Module::include, ::extend, and ::prepend also call ::included, ::extended,
::prepended callbacks, but you can look at the docs for how those work. I
don't know how much these will help, but I have the slides from a few lunch
and learns I gave at work about these topics:
http://jbodah.github.io/blog/2015/09/02/ruby-primer-slides/

Josh

------------------------------
*From:* Maxim hedrovich <maximhedrovich@gmail.com>
*To:* Ruby users <ruby-talk@ruby-lang.org>
*Sent:* Wednesday, January 27, 2016 7:06 AM
*Subject:* Re: extending/including

im think its in your interests to try use documentation more efficiently
because u will do it sooo often .
documentation rly asking on your questions.
if u need information about module methods ==>>>
Class: Module (Ruby 2.2.0) in start of every page
availabe usefull information.. in left side u find methods which are
necessary need to you .
list is not dying at all)

2016-01-27 14:54 GMT+03:00 A Berger <aberger7890@gmail.com>:

Hi
I've read much documentation, still not knowing what these exactly do
--- perhaps getting such answers make this list dying...
If you know where to find the answer you could have put the link to that
paragraph.
Berg
Am 27.01.2016 12:28 schrieb "maxim hedrovich" <hedrovich@list.ru>:

Среда, 27 января 2016, 12:07 +01:00 от A Berger <aberger7890@gmail.com>:

Hi
What are the differences; how are these used?
class << object # doing what exactly?
obj.extend Modul # Mixing in ...
extend Module # ???
include Module
Thank you
Berg

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-t

dude... use documentation for this questions, rly, or read books.
http://ruby-doc.org/

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;