Ruby的细化(refine关键字)

Posted Liz-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ruby的细化(refine关键字)相关的知识,希望对你有一定的参考价值。

  • refine关键字用于局部打开类
  • 使用时用using ModuleNamge
  • 例子来源:《Ruby元编程》2.4.3
  • module StringExtensions
        refine String do
            def reverse
                "esrever"
            end
        end
    end
    
    module StringStuff
        using StringExtensions
        p "my_string".reverse #=>"esrever"
    end
    
    p "my_string".reverse #=>"gnirts_ym"

     

以上是关于Ruby的细化(refine关键字)的主要内容,如果未能解决你的问题,请参考以下文章