Feature #12281
openAllow lexically scoped use of refinements with `using {}` block syntax
Description
In Ruby 2.2.3 a refinement could be used in a begin/end block.
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.
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
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
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.