Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
OpenURI::Meta#charset
charset() { || ... }
lib/open-uri.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.3.0/lib/open-uri.rb, line 549
def charset
type, *parameters = content_type_parse
if pair = parameters.assoc('charset')
pair.last.downcase
elsif block_given?
yield
elsif type && %r{\Atext/} =~ type
"utf-8" # RFC6838 4.2.1
else
nil
end
end
returns a charset parameter in Content-Type field. It is downcased for canonicalization.
If charset parameter is not given but a block is given, the block is called and its result is returned. It can be used to guess charset.
If charset parameter and block is not given, nil is returned except text type. In that case, “utf-8” is returned as defined by RFC6838 4.2.1