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.

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

That darn google again - first hit

ruby include vs ruby prepend

John

···

On Mon, Apr 29, 2013 at 12:21 PM, Love U Ruby <lists@ruby-forum.com> wrote:

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.

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

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]

···

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

@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]

···

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

You asked the same question weeks ago already man!

···

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

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

···

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

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?

···

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

Marc Heiler 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

···

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

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

···

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

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

···

On Tue, Apr 30, 2013 at 9:55 AM, Love U Ruby <lists@ruby-forum.com> 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?