From: Shugo Maeda Date: 2010-11-25T13:35:39+09:00 Subject: [ruby-core:33352] Re: [Ruby 1.9-Feature#4085] Refinements and nested methods Hi, Thanks for you comment. 2010/11/25 Dave Thomas : > �module Quux > � �using FooExt > > � �foo = Foo.new > � �foo.bar �# => FooExt#bar > � �foo.baz �# => Foo#bar > �end > > This behavior makes me nervous�I can see arguments for it, but at the same time I can see in leading to problems, particularly in well structured classes where a large set of behaviors is defined in terms of one method. I'm sure the syntax below is wrong, but look at the spirit of it. > > �module DoubleEach > � �refine Array do > � � �def each > � � � � super do |val| > � � � � � yield 2*val > � � � � end > � � �end > � �end > �end > > �using DoubleEach > > �[ 1, 2, 3 ].each {|v| p v } � #=> 2, 4, 6 > > �[ 1, 2, 3 ].min � �#=> 1 The syntax right and currently [1,2,3].min returns 2. However, It's a bug (methods implemented by C behave wrong), and I think it should return 1 as you showed above. > That would be surprising to me, as I'd expect the behavior of all the Enumerable methods, which depend on each, to change if I change the behavior of each. However, some other person might expect the original behavior of each. For example, class Foo using DoubleEach def bar SomeOtherModule.do_something # DoubleEach may break this method end end If SomeOtherModule.do_something is implemented by someone else, and Array#each is used in do_something, DoubleEach breaks the code. If you'd like to change the behavior of callees, you can use monky patching or singleton methods instead of refinements. -- Shugo Maeda