How does Module#include differ from #prepend?

class Foo
def do_stuff
puts “doing stuff”
end
end

module Wrapper
def do_stuff
puts “before stuff”
super
puts “after stuff”
end
end

class Foo
prepend Wrapper
end

Foo.new.do_stuff
p Foo.ancestors

output:

before stuff
doing stuff
after stuff
[Wrapper, Foo, Object, Kernel, BasicObject]
#----------------------------

class Foo
def do_stuff
puts “doing stuff”
end
end

module Wrapper
def do_stuff
puts “before stuff”
super
puts “after stuff”
end
end

class Foo
include Wrapper
end

Foo.new.do_stuff
p Foo.ancestors

output:

before stuff
doing stuff
after stuff
[Wrapper, Foo, Object, Kernel, BasicObject]

I didn’t get the technical difference between these two. Any one please
help me.

That darn google again - first hit

ruby include vs ruby prepend

John

Love U Ruby you did it wrong again:

class Foo
def do_stuff
puts “doing stuff”
end
end

module Wrapper
def do_stuff
puts “before stuff”
super
puts “after stuff”
end
end

class Foo
include Wrapper
end

Foo.new.do_stuff # doing stuff

p Foo.ancestors #=> [Foo, Wrapper, Object, Kernel, BasicObject]

@Hans sorry for that. Actually I ran the code all at a time,thus result
misleads to me. Now it is set.

class Foo
def do_stuff
puts “doing stuff”
end
end

module Wrapper
def do_stuff
puts “before stuff”
super
puts “after stuff”
end
end

class Foo
prepend Wrapper
end

p Foo.ancestors #=> [Wrapper, Foo, Object, Kernel, BasicObject]

class Foo
def do_stuff
puts “doing stuff”
end
end

module Wrapper
def do_stuff
puts “before stuff”
super
puts “after stuff”
end
end

class Foo
include Wrapper
end

p Foo.ancestors #=> [Foo, Wrapper, Object, Kernel, BasicObject]

You asked the same question weeks ago already man!

Marc H. wrote in post #1107281:

You asked the same question weeks ago already man!

If so then, I am sorry. now I understood these 2 methods difference.

Why it is called that module#prepend is more safer than
alias_method_chain?

Thanks

John W Higgins wrote in post #1107276:

That darn google again - first hit

ruby include vs ruby prepend

John

Yes, I goggled and found below three are useful:

http://globaldev.co.uk/2013/03/ruby-2-0-0-in-detail/

http://dev.af83.com/2012/10/19/ruby-2-0-module-prepend.html

Why it is called that module#prepend is more safer than
alias_method_chain?

On Tue, Apr 30, 2013 at 9:55 AM, Love U Ruby [email protected]
wrote:

module Foobar

Now my question is - Is there any difference between module#prepend and
module#prepend_features? Looking at the output,it seems module#prepend
is set as lower priority call - why so?

prepend_features allows you to override the prepend methodology if you
wish
(and no, I have no idea why anyone would want to - but it’s possible
that
you could do something like never allow a particular method call to be
prepended if needed (and that’s just a guess))

prepended is a callback that tells you a prepend has occured - it is not
“lower priority” - it simply occurs after the fact as opposed to before
as
prepend_features does

module Foobar

def self.prepend_features(mod)
puts “pre-prepending #{self} to #{mod}”
super
end

def self.prepended(mod)
puts “prepending #{self} to #{mod}”
end
end

class Foo
end

class Bar < Foo
prepend Foobar
end

output:
pre-prepending Foobar to Bar
prepending Foobar to Bar

Now my question is - Is there any difference between module#prepend and
module#prepend_features? Looking at the output,it seems module#prepend
is set as lower priority call - why so?