I put another Answer, although the big difference was already pointed out (prcedence/binding), and that can cause hard to find problems (the Tin Man, and others pointed that out).
I think my example shows the problem with a not so usual code snippet, even experienced programmeres do not read like the sunday times:
module I18n
extend Module.new {
old_translate=I18n.method(:translate)
define_method(:translate) do |*args|
InplaceTrans.translate(old_translate, *args)
end
alias :t :translate
}
end
module InplaceTrans
extend Module.new {
def translate(old_translate, *args)
Translator.new.translate(old_translate, *args)
end
}
end
Then i did some code beautifying ...
#this code is wrong!
#just made it 'better looking'
module I18n
extend Module.new do
old_translate=I18n.method(:translate)
define_method(:translate) do |*args|
InplaceTrans.translate(old_translate, *args)
end
alias :t :translate
end
end
if you change the {}
here to do/end
you will get the error, that method translate
does not exist ...
Why this happens is pointed out here more than one - precedence. But where to put braces here? (@the Tin Man: I always use braces, like you, but here ... overseen)
so every answer like
If it's a multi-line block, use do/end
If it's a single line block, use {}
is just wrong if used without the "BUT Keep an eye on braces / precedence!"
again:
extend Module.new {} evolves to extend(Module.new {})
and
extend Module.new do/end evolves to extend(Module.new) do/end
(what ever the result of extend does with the block ...)
So if you want to use do/end use this:
#this code is ok!
#just made it 'better looking'?
module I18n
extend(Module.new do
old_translate=I18n.method(:translate)
define_method(:translate) do |*args|
InplaceTrans.translate(old_translate, *args)
end
alias :t :translate
end)
end