[ruby-core:127240] [Ruby Feature#22474] Kernel#using (or BasicObject#using)
Issue #22474 has been reported by UlyssesZhan (Ulysses Zhan). ---------------------------------------- Feature #22474: Kernel#using (or BasicObject#using) https://bugs.ruby-lang.org/issues/22474 * Author: UlyssesZhan (Ulysses Zhan) * Status: Open ---------------------------------------- Currently there is `Module#using`. The scope where `using` is effective is the lexical scope of the module eval block (blocks for `module` keyword, `Module::new`, `module_eval`, and `module_exec`). However, since any block is a lexical scope, why not allow `using` in any block? Example: ```ruby module R refine(Enumerable) { def f = 1 } end proc { using R; [].f }.() # => 1 [].f # => ! NoMethodError ``` Note that we can already achieve a similar effect in pure Ruby: it is possible to define a function `while_using`. ```ruby module Kernel private def while_using mod, binding, code old = Thread.current[:using_mod] Thread.current[:using_mod] = mod args = binding.local_variables.map { binding.local_variable_get _1 } result = TOPLEVEL_BINDING.eval(<<~RUBY).bind_call binding.receiver, *args Module.new do using Thread.current[:using_mod] def run #{binding.local_variables.join ', '} #{code} end end.instance_method :run RUBY Thread.current[:using_mod] = old result end end module R refine(Enumerable) { def f = 1 } end class C def g x @y = 1 while_using R, binding, 'x + @y + [].f' end end C.new.g 1 # => 3 ``` -- https://bugs.ruby-lang.org/
Issue #22474 has been updated by alanwu (Alan Wu). Status changed from Open to Feedback
However, since any block is a lexical scope, why not allow using in any block?
Addressed by #22097 yes? ---------------------------------------- Feature #22474: Kernel#using (or BasicObject#using) https://bugs.ruby-lang.org/issues/22474#change-119660 * Author: UlyssesZhan (Ulysses Zhan) * Status: Feedback ---------------------------------------- Currently there is `Module#using`. The scope where `using` is effective is the lexical scope of the module eval block (blocks for `module` keyword, `Module::new`, `module_eval`, and `module_exec`). However, since any block is a lexical scope, why not allow `using` in any block? Example: ```ruby module R refine(Enumerable) { def f = 1 } end proc { using R; [].f }.() # => 1 [].f # => ! NoMethodError ``` Note that we can already achieve a similar effect in pure Ruby: it is possible to define a function `while_using`. ```ruby module Kernel private def while_using mod, binding, code old = Thread.current[:using_mod] Thread.current[:using_mod] = mod args = binding.local_variables.map { binding.local_variable_get _1 } result = TOPLEVEL_BINDING.eval(<<~RUBY).bind_call binding.receiver, *args Module.new do using Thread.current[:using_mod] def run #{binding.local_variables.join ', '} #{code} end end.instance_method :run RUBY Thread.current[:using_mod] = old result end end module R refine(Enumerable) { def f = 1 } end class C def g x @y = 1 while_using R, binding, 'x + @y + [].f' end end C.new.g 1 # => 3 ``` -- https://bugs.ruby-lang.org/
Issue #22474 has been updated by UlyssesZhan (Ulysses Zhan). alanwu (Alan Wu) wrote in #note-2:
Seems to be already addressed by #22097.
I see. My quick issue search did not dig it up. I would say `using R; something` is much easier than `-> { something }.refined(R).()`, and we can have both features. However `Proc#refined` should be good enough for the former use case. ---------------------------------------- Feature #22474: Kernel#using (or BasicObject#using) https://bugs.ruby-lang.org/issues/22474#change-119662 * Author: UlyssesZhan (Ulysses Zhan) * Status: Feedback ---------------------------------------- Currently there is `Module#using`. The scope where `using` is effective is the lexical scope of the module eval block (blocks for `module` keyword, `Module::new`, `module_eval`, and `module_exec`). However, since any block is a lexical scope, why not allow `using` in any block? Example: ```ruby module R refine(Enumerable) { def f = 1 } end proc { using R; [].f }.() # => 1 [].f # => ! NoMethodError ``` Note that we can already achieve a similar effect in pure Ruby: it is possible to define a function `while_using`. ```ruby module Kernel private def while_using mod, binding, code old = Thread.current[:using_mod] Thread.current[:using_mod] = mod args = binding.local_variables.map { binding.local_variable_get _1 } result = TOPLEVEL_BINDING.eval(<<~RUBY).bind_call binding.receiver, *args Module.new do using Thread.current[:using_mod] def run #{binding.local_variables.join ', '} #{code} end end.instance_method :run RUBY Thread.current[:using_mod] = old result end end module R refine(Enumerable) { def f = 1 } end class C def g x @y = 1 while_using R, binding, 'x + @y + [].f' end end C.new.g 1 # => 3 ``` -- https://bugs.ruby-lang.org/
participants (2)
-
alanwu (Alan Wu) -
UlyssesZhan (Ulysses Zhan)