Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby for Rails p.462-464 - why include vs. extend?

Reply
Thread Tools

Ruby for Rails p.462-464 - why include vs. extend?

 
 
Jeff Cohen
Guest
Posts: n/a
 
      10-16-2006
Making my way through Ruby for Rails (excellent book, David), but am
puzzled by the explanation of why Rails jumps through hoops to turn
Module instance methods into class methods.

On p. 463, David represents the situation sans Rails:

module A
module M
module ClassMethods
def some_method
#...
end

def included(c)
c.extend(ClassMethods)
end
end
end

and then a class later that does this:

class B
include A::M
end

The goal as for M::ClassMethods to become class methods of B. Whew.

Here are my questions:

1. Why can't class B just extend A::M::ClassMethods? Why use include
and therefore necessitate this whole indirect approach? Must be some
advantage that I'm not seeing?

In other words, why couldn't this have worked?

class B
extend A::M::ClassMethods
end

and then there's no need M::included() needed at all?

2. I guess because M::included() was overridden, class B did not get any
instance methods - which I think would have normally been the case with
an include statement. SO what if you wanted the "normal" inclusion
behavior, but also wanted to do something "extra" when your module is
included?

Thanks!
Jeff

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Jan Svitok
Guest
Posts: n/a
 
      10-16-2006
On 10/16/06, Jeff Cohen <> wrote:
> Making my way through Ruby for Rails (excellent book, David), but am
> puzzled by the explanation of why Rails jumps through hoops to turn
> Module instance methods into class methods.
>
> On p. 463, David represents the situation sans Rails:
>
> module A
> module M
> module ClassMethods
> def some_method
> #...
> end
>
> def included(c)
> c.extend(ClassMethods)
> end
> end
> end
>
> and then a class later that does this:
>
> class B
> include A::M
> end
>
> The goal as for M::ClassMethods to become class methods of B. Whew.
>
> Here are my questions:
>
> 1. Why can't class B just extend A::M::ClassMethods? Why use include
> and therefore necessitate this whole indirect approach? Must be some
> advantage that I'm not seeing?
>
> In other words, why couldn't this have worked?
>
> class B
> extend A::M::ClassMethods
> end
>
> and then there's no need M::included() needed at all?
>
> 2. I guess because M::included() was overridden, class B did not get any
> instance methods - which I think would have normally been the case with
> an include statement. SO what if you wanted the "normal" inclusion
> behavior, but also wanted to do something "extra" when your module is
> included?
>
> Thanks!
> Jeff


1. The reason in unification - you don't need to remember where to use
include and where extend, and this way you get both instance and class
methods.

2. overriding include should have no impact on actual method
including. It's just a callback. All the stuff is being done in
append_features. (At least that's what documentation says.)
So either the docs are wrong, or your problem is somewhere else.

 
Reply With Quote
 
 
 
 
dblack@wobblini.net
Guest
Posts: n/a
 
      10-16-2006
Hi --

On Mon, 16 Oct 2006, Jeff Cohen wrote:

> Making my way through Ruby for Rails (excellent book, David), but am
> puzzled by the explanation of why Rails jumps through hoops to turn
> Module instance methods into class methods.
>
> On p. 463, David represents the situation sans Rails:
>
> module A
> module M
> module ClassMethods
> def some_method
> #...
> end
>
> def included(c)
> c.extend(ClassMethods)
> end
> end
> end
>
> and then a class later that does this:
>
> class B
> include A::M
> end
>
> The goal as for M::ClassMethods to become class methods of B. Whew.
>
> Here are my questions:
>
> 1. Why can't class B just extend A::M::ClassMethods? Why use include
> and therefore necessitate this whole indirect approach? Must be some
> advantage that I'm not seeing?


The goal is to be able to do one "include" and have both instance
methods and class methods added to the class that's doing the
including.

> In other words, why couldn't this have worked?
>
> class B
> extend A::M::ClassMethods
> end
>
> and then there's no need M::included() needed at all?


You'd have to do:

class B
include A::M
extend A::M::ClassMethods
end

to get the same effect.

> 2. I guess because M::included() was overridden, class B did not get any
> instance methods - which I think would have normally been the case with
> an include statement. SO what if you wanted the "normal" inclusion
> behavior, but also wanted to do something "extra" when your module is
> included?


You still get the normal behavior -- that is, class B will still mix
in any instance methods defined in A::M.


David

--
David A. Black |
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

 
Reply With Quote
 
Jeff Cohen
Guest
Posts: n/a
 
      10-16-2006
unknown wrote:
> You still get the normal behavior -- that is, class B will still mix
> in any instance methods defined in A::M.
>


Got it! Sorry for my oversight.

Thanks,
Jeff

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
/* #include <someyhing.h> */ => include it or do not include it?That is the question .... Andreas Bogenberger C Programming 3 02-22-2008 10:53 AM
Ruby on Rails Developer – London – Upto £55k – Ruby on Rails Jason Wong Ruby 0 11-20-2007 11:01 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Rails and Ruby in Germany -> Rails is Ruby killer application Josef 'Jupp' SCHUGT Ruby 5 01-24-2006 11:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57