次のコードが実際に機能する理由は少し混乱しています。
String.instance_eval do # self is set to String
[:readlink, :symlink?, :expand_path].each do |method| # self is still String
define_method(method) do # self is still String
File.send(method, self) # what exactly is this self?
end
end
end
"asdf".expand_path # => "C:/users/some_user/asdf"
最後の行がなぜ機能するのかわかりません。File.send(method, String)
各メソッドが定義されている場合、メソッドの本体は?と同等ではありません。上記のブロックはどれも実際には変更されませんself
。変更される唯一の行self
はでString.instance_eval
あり、に変更self
されString
ます。