Method: Gem::Net::HTTPHeader#fetch
- Defined in:
- lib/rubygems/net-http/lib/net/http/header.rb
#fetch(key, *args, &block) ⇒ Object
call-seq:
fetch(key, default_val = nil) {|key| ... } -> object
fetch(key, default_val = nil) -> value or default_val
With a block, returns the string value for key if it exists; otherwise returns the value of the block; ignores the default_val; see Fields:
res = Gem::Net::HTTP.get_response(hostname, '/todos/1')
# Field exists; block not called.
res.fetch('Connection') do |value|
fail 'Cannot happen'
end # => "keep-alive"
# Field does not exist; block called.
res.fetch('Nosuch') do |value|
value.downcase
end # => "nosuch"
With no block, returns the string value for key if it exists; otherwise, returns default_val if it was given; otherwise raises an exception:
res.fetch('Connection', 'Foo') # => "keep-alive"
res.fetch('Nosuch', 'Foo') # => "Foo"
res.fetch('Nosuch') # Raises KeyError.
341 342 343 344 |
# File 'lib/rubygems/net-http/lib/net/http/header.rb', line 341 def fetch(key, *args, &block) #:yield: +key+ a = @header.fetch(key.downcase.to_s, *args, &block) a.kind_of?(Array) ? a.join(', ') : a end |