views:

59

answers:

1

This is a large commit. But I want you to concentrate on this change block. http://github.com/rails/rails/commit/d916c62cfc7c59ab6411407a05b946d3dd7535e9#L2L1304

Even without understanding the full context of the code I am not able to think of a scenario where I would use

include Module.new {
  class_eval <<-RUBY
    def foo
       puts 'foo'
    end
  RUBY
}

Then end result is that in the root context (self just before include Module.new) a method called foo has been added.

If I take out the Module.new code and if I only leave class_eval in that case also I will have a method called foo in self.

What am I missing.

+2  A: 

If you dig in the documentation you find that including a module will add the methods therein only if they are not already defined. So this approach will not overwrite the method in case it is already there.

Jakub Hampl
Thanks for the answer.
Nadal
look at this pastie http://pastie.org/956409 . Class will include the module only if that module is not already added. Since this is a brand new anonymous module it will always get added. another one http://pastie.org/956410
Nadal
Well I haven't tried it - I just read the docs (and remembered that The Humble Ruby Book mentioned something like this). I guess there must be another reason there - or not, maybe it's just code chaos?
Jakub Hampl