From: shevegen@... Date: 2016-04-17T07:34:29+00:00 Subject: [ruby-core:74987] [Ruby trunk Feature#12281] Allow lexically scoped use of refinements with `using {}` block syntax Issue #12281 has been updated by Robert A. Heiler. Not having any pro or contra opinion here but I would like to just briefly chime in that I find the syntax quite heavy. module Foo refine String do It feels a bit ... odd with other ruby code that I would use or write, to suddenly have a constant after a method or keyword, and then a block. Perhaps I am just not used to it but my brain seems to take longer. I wonder if we could have some other way for refinements but I digress - sorry for the semi off-topic part from me here. ---------------------------------------- Feature #12281: Allow lexically scoped use of refinements with `using {}` block syntax https://bugs.ruby-lang.org/issues/12281#change-58114 * 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: