From: Shugo Maeda Date: 2010-11-26T11:24:43+09:00 Subject: [ruby-core:33396] Re: [Ruby 1.9-Feature#4085][Open] Refinements and nested methods Hi, 2010/11/26 Magnus Holm : > Woah, this is very nice stuff! Some comments/questions: > > 1. Could you give an example of how it would behave if it had local > rebinding as in classbox? If it had local rebinding, the following code would print "Quux::Foo#bar". class Foo def bar puts "Foo#bar" end def baz bar end end module Quux refine Foo do def bar puts "Quux::Foo#bar" end end end using Quux foo = Foo.new foo.baz > 2. I don't like the idea of having both #using and #include. I don't > want to think about which one to use; I just want to use the module! I > think this is getting even more messy than with the new mixin-feature. > Even right *now* people only use #include (and def included(mod); > mod.extend ClassMethods; end) because That's How Modules Work�. Matz doesn't like it, but I think it's worth considering. However, it's a problem that we have main#include, where main is self at the top-level, but not Kernel#include now. > 3. Is this expected behaviour? > > module Ext > �refine(Object) do > � �def to_json; "something"; end > �end > end > > Fixnum.send(:using, Ext) > "Hello".to_json # => Works because #using worked at the file scope It is intended. > This makes it impossible to add refinements within a included-callback. If a binding is passed to include, it may be possible as follows: module Foo refine XXX do ... end def do_something end ... def self.included(klass, binding) eval("using Foo", binding) end end class Bar include Foo # both do_something and refinements are available end It's necessary to check the arity of included for backward compatibility. Currenty, you can use a used-callback instead. module Foo refine XXX do ... end def do_something end ... def self.used(klass) klass.send(:include, self) end end class Bar using Foo end -- Shugo Maeda