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

singleton method URI.for

for(scheme, *arguments, default: URI::Generic) -> URI::GenericRuby 3.0 から[permalink][rdoc][edit]

scheme と arguments から新しい URI オブジェクトを生成して返します。

scheme を大文字にしたものが URI.scheme_list に登録されていれば、そのクラスのインスタンスを生成します。登録されていない場合は default で指定したクラスを使います。arguments はそのクラスの new にそのまま渡されます。

[PARAM] scheme:
生成する URI の scheme を文字列で指定します。
[PARAM] arguments:
構成要素を、生成に使うクラスの new に渡す引数と同じ形式で指定します。
[PARAM] default:
scheme に対応するクラスが登録されていない場合に使うクラスを指定します。省略した場合は URI::Generic です。
require 'uri'
values = ["john.doe", "www.example.com", "123", nil, "/forum/questions/", nil, "tag=networking&order=newest", "top"]
p URI.for('https', *values)
# => #<URI::HTTPS https://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top>
p URI.for('foo', *values, default: URI::HTTP)
# => #<URI::HTTP foo://john.doe@www.example.com:123/forum/questions/?tag=networking&order=newest#top>

[SEE_ALSO] URI.scheme_list, URI::Generic

[SEE_ALSO] URI.register_scheme