このマニュアルは既にメンテナンスが終了したバージョンの Ruby を対象としています。 最新版のマニュアルへ

Ruby 3.2 リファレンスマニュアル

class Refinement

[edit]

要約

Module#refine のブロックの中の self のクラスです。

Refinement#import_methodsで他のモジュールからメソッドをインポートできます。

目次

インスタンスメソッド
privateメソッド

継承しているメソッド

Moduleから継承している特異メソッド
Moduleから継承しているメソッド

インスタンスメソッド

refined_class -> ClassRuby 3.2 から Ruby 3.4 で削除[permalink][rdoc][edit]

selfModule#refine の対象にしているクラスを返します。

module M
  refine String do
  end
end

p M.refinements[0].refined_class # => String

[SEE_ALSO] Module#refinements, Module#refine

privateメソッド

import_methods(*modules) -> selfRuby 3.1 から[permalink][rdoc][edit]

モジュールからメソッドをインポートします。

Module#includeと違って、import_methods はメソッドをコピーして refinement に追加して、refinementでインポートしたメソッドを有効化します。

メソッドをコピーするため、Rubyコードで定義されたメソッドだけしかインポートできないことに注意してください。

module StrUtils
  def indent(level)
    ' ' * level + self
  end
end

module M
  refine String do
    import_methods StrUtils
  end
end

using M
p "foo".indent(3) # => "   foo"

module M
  refine String do
    import_methods Enumerable
    # Can't import method which is not defined with Ruby code: Enumerable#drop
  end
end