From: 6ftdan@... Date: 2016-04-14T03:48:01+00:00 Subject: [ruby-core:74944] [Ruby trunk Feature#12281] Allow lexically scoped use of refinements with `using {}` block syntax Issue #12281 has been reported by Daniel P. Clark. ---------------------------------------- Feature #12281: Allow lexically scoped use of refinements with `using {}` block syntax https://bugs.ruby-lang.org/issues/12281 * Author: Daniel P. Clark * Status: Open * Priority: Normal * Assignee: ---------------------------------------- In Ruby 2.2.3 a refinement could be used in a begin/end block. ~~~ruby module Moo refine Fixnum do def to_s "moo" end end end begin # valid Ruby 2.2.3 and NOT Ruby 2.3 using Moo 1.to_s end # => "moo" ~~~ Since this use case has been removed I would like to propose an alternative. ~~~ruby using Moo do 1.to_s end # => "moo" ~~~ I would like to propose allowing refinements to take a block and perform the refinement within the block and work just as if it were in it's own lexically scoped class. I've been writing a lot of Rust lately and have found that their way of implementing Traits is just like Ruby's refinements except for that you can use Rust's version of refinements anywhere. Since Ruby's implementation is strictly lexically scoped I merely suggest a block syntax for `using` to allow greater expansion of refinements. ~~~rust // Rust impl MyCapitalize for String { fn my_capitalize(&self) -> Self { // code here } } use MyCapitalize; String::from("hello").my_capitalize() ~~~ Rust lets you use the "refinement" of the trait implementation anywhere you use `use` just like Ruby's `using`. But currently Ruby restricts where `using` can be used. I would like that restriction to be lifted by allowing `using` to take a block. ~~~ruby # Ruby module MyCapitalize refine String do def my_capitalize # code here end end end using MyCapitalize do "hello".my_capitalize end # => "Hello" ~~~ This way we keep Ruby's strict lexical scope behavior and at the same time allow refinement usage anywhere we need it. -- https://bugs.ruby-lang.org/ Unsubscribe: