Project

General

Profile

Feature #14223

Updated by osyo (manga osyo) over 6 years ago

Refinements に関する提案です。   
 現行の仕様では Refinements で定義された `#to_proc` は `&hoge` 時に暗黙的に呼びだされません。   

 ```ruby 
 class X 
 end 

 using Module.new { 
	 refine String do 
		 def to_proc 
			 proc { |it| it.send self } 
		 end 

		 def refine_method 
			 "X#refine_method" 
		 end 
	 end 
 } 

 def func &block 
 end 

 p "upcase".refine_method 
 # => "X#refine_method" 

 p "upcase".to_proc.call "homu" 
 # => "HOMU" 

 # Error: wrong argument type X (expected Proc) (TypeError) 
 func &"upcase" 
 ``` 

 実行結果:https://wandbox.org/permlink/j8Hhavy7LoKYjrnz 実行結果:https://wandbox.org/permlink/sbvsCeXwXzwp1WxA 


 最近の傾向として `Kernel#send` や式展開時の `#to_s` などで Refinements が有効になっています。 
 そういう意味では `&hoge` で暗黙的に呼び出される `#to_proc` も Refinements が有効になってもよいのではないでしょうか。   
 この件に関して、他の方の意見を聞かせていただけると助かります。   
 また、自分で実装を書いてみたのですが『とりあえず動いている』というレベルなので、Refinements の実装に詳しい方がいればパッチもみていただけると助かります。   

Back