Extending/including

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

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.

···

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

class << object # doing what exactly?

You can look for "eigenclass" or "singleton class".
See
http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/374765?374647-375967
http://www.integralist.co.uk/posts/eigenclass.html

obj.extend Modul # Mixing in ...

Instance methods defined in a module become methods of obj.

extend Module # ???

Same as before, but instead of obj it's "main" - the object you see
when accessing self at top level.

include Module

Similar effect as the one above but different effect on scoping:
constants defined in the module scope are now accessible on top level.

Try it out in a script or irb. That way you learn most.

Kind regards

robert

···

On Wed, Jan 27, 2016 at 12:07 PM, A Berger <aberger7890@gmail.com> wrote:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.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
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

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;

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
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

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;

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 ==>>> http://ruby-doc.org/core-2.2.0/Module.html 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 ModuleThank 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>

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

im rly novice in ruby and programming , my english is terribly bad, but i
can find answer... its not arrogance or something, i try to help u find
obvious answers oneself.

Best regards.

···

2016-01-27 15:06 GMT+03:00 Maxim hedrovich <maximhedrovich@gmail.com>:

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 ==>>>
http://ruby-doc.org/core-2.2.0/Module.html 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
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

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;