Method: Iri#cut

Defined in:
lib/iri.rb

#cut(path = '/') ⇒ Iri

Removes the entire path, query, and fragment parts and sets a new path.

This method is useful for “cutting off” everything after the host:port and setting a new path, effectively removing query string and fragment.

Examples:

Cutting off path/query/fragment and setting a new path

Iri.new('https://google.com/a/b?q=test').cut('/hello')
# => "https://google.com/hello"

Resetting to root path

Iri.new('https://google.com/a/b?q=test#section2').cut()
# => "https://google.com/"

Parameters:

  • path (String) (defaults to: '/')

    New path to set, defaults to “/”

Returns:

  • (Iri)

    A new Iri instance

Raises:

  • (ArgumentError)

See Also:



344
345
346
347
348
349
350
351
352
353
# File 'lib/iri.rb', line 344

def cut(path = '/')
  raise ArgumentError, "The path can't be nil" if path.nil?
  path = path.to_s
  raise ArgumentError, "The path can't be empty" if path.empty?
  modify do |c|
    c.query = nil
    c.path = path
    c.fragment = nil
  end
end