Method: Iri#append

Defined in:
lib/iri.rb

#append(part) ⇒ Iri

Appends a new segment to the existing path.

This method adds a new segment to the existing path, automatically handling the slash between segments and URL encoding the new segment.

Examples:

Appending a path segment

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

Appending to a path with a trailing slash

Iri.new('https://example.com/a/').append('hello')
# => "https://example.com/a/hello?q=test"

Appending a segment that needs URL encoding

Iri.new('https://example.com/docs').append('section 1')
# => "https://example.com/docs/section%201"

Parameters:

  • part (String, #to_s)

    New segment to add to the existing path

Returns:

  • (Iri)

    A new Iri instance

Raises:

  • (ArgumentError)

See Also:



375
376
377
378
379
380
381
382
383
# File 'lib/iri.rb', line 375

def append(part)
  raise ArgumentError, "The part can't be nil" if part.nil?
  part = part.to_s
  raise ArgumentError, "The part can't be empty" if part.empty?
  modify do |c|
    tail = (c.path.end_with?('/') ? '' : '/') + CGI.escape(part.to_s)
    c.path = c.path + tail
  end
end