Ruby 3.2 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Refinementクラス > import_methods (private)
import_methods(*modules) -> self
[permalink][rdoc]モジュールからメソッドをインポートします。
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