From: bigbadmath@...
Date: 2014-08-28T14:44:07+00:00
Subject: [ruby-core:64612] [ruby-trunk - misc #10178] refinements unactivated within refine block scope?

Issue #10178 has been updated by Alexander Moore-Niemi.


Nobuyoshi Nakada wrote:
> In general, the scope inside a method definition is different from outside.
> Consider method arguments and class/module level local variables.

Finally, is there a reason I can't call `using` within the 2nd refine block? I can't invoke it within that scope, right?

----------------------------------------
misc #10178: refinements unactivated within refine block scope?
https://bugs.ruby-lang.org/issues/10178#change-48531

* Author: Alexander Moore-Niemi
* Status: Closed
* Priority: Low
* Assignee: 
* Category: 
* Target version: current: 2.2.0
----------------------------------------
I doubt I am seeing a bug, but I was hoping someone could clarify for me the reason why I am seeing what I see. I tried pouring over the spec and wasn't quite able to pin it down.

My use case of refinements is not the normal one, so this is not high priority by any means.

But I am curious why, if I have defined a refinement in, say, module A, and then module B is using A, if B itself as a refine block, A's refinements will not be active within it.

So:

~~~ruby
module A
  refine Time
  def weekday
    self.strftime("%A")
  end
end

module B
  using A
  puts Time.now.weekday # 1
  refine ActiveSupport::Time
  def method_missing(method, *args)
    puts Time.now.weekday # 2
    self.to_time.send(method.to_sym, args.first)
  end
  puts Time.now.weekday # 3
end
~~~

1 and 3 will be defined, but 2 will not. Is it because according to:
"The scope of a refinement is lexical in the sense that, when control is transferred outside the scope (e.g., by an invocation of a method defined outside the scope, by load/require, etc...), the refinement is deactivated."
refine transfers control outside the scope of the module, so no matter where I put using, it will not have the refinements of A active?

I apologize for my ignorance and greatly appreciate your answers on this matter.




-- 
https://bugs.ruby-lang.org/