#append_features deprecated?

Somone else mentioned this. Is it true?

Thanks,
T.

Trans wrote:

Somone else mentioned this. Is it true?

This says Module#included " should be used in preference to
Module.append_features if your code wants to perform some action when a
module is included in another."

http://www.ruby-doc.org/core/classes/Module.html#M000704

Cheers,
Dave

Dave Burt wrote:

Trans wrote:

Somone else mentioned this. Is it true?

This says Module#included " should be used in preference to Module.append_features if your code wants to perform some action when a module is included in another."

class Module - RDoc Documentation

Cheers,
Dave

   module M
     def self.included(klass)
       puts "Included in #{klass}"
     end
   end

   class A
     include M # -> Included in A
   end

   class B
     M.append_features(self) # Nothing
   end

As you can see, Module#append_features doesn't call the .included method on the module, include does.

Cheers,
Daniel

As you can see, Module#append_features doesn't call the .included method
on the module, include does.

Well, it's best to see it like this :

moulon% cat b.rb
#!/usr/bin/ruby
module M
   def self.included(klass)
      puts "Included in #{klass}"
   end
   def a
      puts "a"
   end
end

class A
   include M
end

A.new.a
moulon%

moulon% ./b.rb
Included in A
a
moulon%

moulon% cat b.rb
#!/usr/bin/ruby
module M
   def self.append_features(klass)
      puts "Included in #{klass}"
   end
   def a
      puts "a"
   end
end

class A
   include M
end

A.new.a
moulon%

moulon% ./b.rb
Included in A
./b.rb:15: undefined method `a' for #<A:0xb7d64b38> (NoMethodError)
moulon%

Guy Decoux

Daniel Schierbeck wrote:

Dave Burt wrote:

Trans wrote:

Somone else mentioned this. Is it true?

This says Module#included " should be used in preference to Module.append_features if your code wants to perform some action when a module is included in another."

class Module - RDoc Documentation

Cheers,
Dave

  module M
    def self.included(klass)
      puts "Included in #{klass}"
    end
  end

  class A
    include M # -> Included in A
  end

  class B
    M.append_features(self) # Nothing
  end

As you can see, Module#append_features doesn't call the .included method on the module, include does.

Cheers,
Daniel

Oooops, #append_features is private, so it should be

   M.send :append_features, self

Cheers,
Daniel

ts wrote:

> As you can see, Module#append_features doesn't call the .included method > on the module, include does.

Well, it's best to see it like this :

moulon% cat b.rb
#!/usr/bin/ruby
module M
   def self.included(klass)
      puts "Included in #{klass}"
   end
   def a
      puts "a"
   end
end

class A
   include M
end

A.new.a
moulon%

moulon% ./b.rb
Included in A
a
moulon%
moulon% cat b.rb
#!/usr/bin/ruby
module M
   def self.append_features(klass)
      puts "Included in #{klass}"
   end
   def a
      puts "a"
   end
end

class A
   include M
end

A.new.a
moulon%

moulon% ./b.rb
Included in A
/b.rb:15: undefined method `a' for #<A:0xb7d64b38> (NoMethodError)
moulon%

Guy Decoux

Um, yeah. `append_features' does the actual work (appending the methods of a module to a class/module), `include' just calls `append_features' and then `included'.

   class Module
     def include(*mods)
       mods.each do |mod|
         mod.append_features(self)
         mod.included(self)
       end
     end
   end

Cheers,
Daniel

Um, yeah. `append_features' does the actual work (appending the methods
of a module to a class/module), `include' just calls `append_features'
and then `included'.

yes, and this is why you must call super in #append_features (if you
redefine it) otherwise ruby don't include the module.

Guy Decoux

Daniel Schierbeck wrote:

Um, yeah. `append_features' does the actual work (appending the methods
of a module to a class/module), `include' just calls `append_features'
and then `included'.

   class Module
     def include(*mods)
       mods.each do |mod|
         mod.append_features(self)
         mod.included(self)
       end
     end
   end

It does? Looking at the C code it seems a lot more complicated then
that.

T.

ts wrote:

> Um, yeah. `append_features' does the actual work (appending the methods > of a module to a class/module), `include' just calls `append_features' > and then `included'.

yes, and this is why you must call super in #append_features (if you
redefine it) otherwise ruby don't include the module.

Guy Decoux

Yup.

Trans wrote:

Daniel Schierbeck wrote:

Um, yeah. `append_features' does the actual work (appending the methods
of a module to a class/module), `include' just calls `append_features'
and then `included'.

   class Module
     def include(*mods)
       mods.each do |mod|
         mod.append_features(self)
         mod.included(self)
       end
     end
   end

It does? Looking at the C code it seems a lot more complicated then
that.

T.

I'm sure there's more to it, but from a user's perspective I believe that's what's happening :wink:

Cheers,
Daniel