append_features(mod) -- mod.kind_of? makes absolutely no sense

T

T. Onoma

First of all, if class Class inherits class Module then why isn't
append_features defined for Class?

class Whatever
append_features MyModule
end
# => undefined method `append_features' for Whatever:Class (NoMethodError)

But even more nonsensical:

class Module
alias append_features_orig append_features
def append_features(mod)
p mod, mod.class
append_features_orig(mod)
end
end
module MyModule
def wow
puts "You have to be kidding!"
end
end
class Test
include MyModule
end
t = Test.new
t.wow

Produces:

Test
Class
You have to be kidding!

Its my understanding the include passes each Module to append_features, but
that's not what append_features is saying. In fact, according to this
append_features is appending Test to Test, even though it works fine. Please,
tell me I'm over looking the obvious here.
 
N

nobu.nokada

Hi,

At Sun, 14 Dec 2003 16:45:41 +0900,
T. Onoma said:
class Module
alias append_features_orig append_features
def append_features(mod)
p mod, mod.class p self # try this.
append_features_orig(mod)
end
end
module MyModule
def wow
puts "You have to be kidding!"
end
end
class Test
include MyModule
end
t = Test.new
t.wow
 
C

Christoph

T. Onoma said:
First of all, if class Class inherits class Module then why isn't
append_features defined for Class?

class Whatever
append_features MyModule
end
# => undefined method `append_features' for Whatever:Class
(NoMethodError)

Isn't this the whole point of Module#undef (or undef_method)?

class A
def foo
end
end
class B < A
undef :foo
end

B.new.foo # undefined method `foo' ...

....
Its my understanding the include passes each Module to append_features, but
that's not what append_features is saying. In fact, according to this
append_features is appending Test to Test, even though it works fine. Please,
tell me I'm over looking the obvious here.

Don't worry, you are overlooking the obvious:)

class Module
alias append_features_orig append_features
def append_features(mod)
# changed from p mod, mod.class
p mod, self
append_features_orig(mod)
end
end
module MyModule
def wow
puts "You have to be kidding!"
end
end
class Test
include MyModule
end
t = Test.new
t.wow

/Christoph
 
T

T. Onoma

Hi,

At Sun, 14 Dec 2003 16:45:41 +0900,



p self # try this.

Okay, that at least clarifies where MyModule went. How did it become self? In
#include it is the argument. Let me guess:

def include(mod)
mod.append_features(self)
end

By the way, PickAxe (my Bible) doesn't explain this correctly.
 
N

nobu.nokada

Hi,

At Mon, 15 Dec 2003 01:17:04 +0900,
T. Onoma said:
Okay, that at least clarifies where MyModule went. How did it become self? In
#include it is the argument. Let me guess:

def include(mod)
mod.append_features(self)
end

More precisely,

def include(*modules)
modules.reverse_each do |mod|
mod.append_features(self)
mod.included(self)
end
end
 
D

Dave Thomas

def include(mod)
mod.append_features(self)
end

By the way, PickAxe (my Bible) doesn't explain this correctly.

Perhaps you might be more specific? I'm always interested in improving
the text.


Cheers

Dave
 
C

Christoph

T. Onoma wrote:
....
def include(mod)
mod.append_features(self)
end

By the way, PickAxe (my Bible) doesn't explain this correctly.

Sure it does (unless you own a first edition)

/Christoph
 
T

T. Onoma

Perhaps you might be more specific? I'm always interested in improving
the text.

Hi Dave,

According to Christoph I may just own a first addition. But just the same, my
confusion came from pg. 350 in chp. 22 Built-in Classes, it says about
Module.include(<aModule>+):

Invokes Module.append_features on each parameter in turn.

And under Module.append_features(aModule) is says:

The contants and methods of aModule are added to the current module...

This is ambigious b/c, before I knew, it read to me as if "Invokes
Module.append_features on each parameter" meant append_features(aModule) not
aModule.append_features(self). Which is exactly why I didn't understand why
one couldn't use append_features like this:

module AModule
append_features(AnotherModule)
end

When what you really need to do is:

module AModule
AnotherModule.append_features(self)
end

Perhaps if you just added to Module.include, Nakada's "More precisely",

  def include(*modules)
    modules.reverse_each do |mod|
      mod.append_features(self)
      mod.included(self)
    end
  end

That would make all the difference.

Thanks for listening,
T.
 
C

Christoph

T. Onoma wrote:
....
Invokes Module.append_features on each parameter in turn.

And under Module.append_features(aModule) is says:

The contants and methods of aModule are added to the current module...

FYI - the online PickAxe version reads as:

When this module is included in another, Ruby calls append_features in this
module, passing it the receiving module in aModule. Ruby's default
implementation is to add the constants, methods, and module variables of
this module to aModule if this module has not already been added to aModule
or one of its ancestors. See also Module#include on page 345.

....
Perhaps if you just added to Module.include, Nakada's "More precisely",

def include(*modules)
modules.reverse_each do |mod|
mod.append_features(self)
mod.included(self)
end
end

That would make all the difference.

I agree adding this Module#include implementation might
be helpful - perhaps even the "privacy-aware" version
(note that the Module#included hook was (afaik) never added
in the 1.6 series)

class Module
private
def include(*modules)
modules.reverse_each do |mod|
mod.__send__:)append_features, self)
mod.__send__:)included, self)
end
end
end

/Christoph
 
D

Dave Thomas

This is ambigious b/c, before I knew, it read to me as if "Invokes
Module.append_features on each parameter" meant
append_features(aModule) not
aModule.append_features(self). Which is exactly why I didn't
understand why
one couldn't use append_features like this:

Yes - I see what you're saying: the text was too concise. In fact after
seeing nobu's post I added his example to the text: look for it in an
upcoming 'ri'.

Cheers


Dave

Cheers

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top