From: "Dan0042 (Daniel DeLorme)" Date: 2022-02-22T14:13:55+00:00 Subject: [ruby-core:107713] [Ruby master Feature#18594] Add a #to_h method on URI::Generic Issue #18594 has been updated by Dan0042 (Daniel DeLorme). I'm not opposed, but what do you use the hash for? What's the use case? If it's for serialization, then normally you'd serialize the URI to a string and use URI parsing on the other side e.g. javascript `(new URL(str)).path` ---------------------------------------- Feature #18594: Add a #to_h method on URI::Generic https://bugs.ruby-lang.org/issues/18594#change-96636 * Author: jimcavoli (Jim Cavoli) * Status: Open * Priority: Normal ---------------------------------------- It's just surprisingly challenging to get a hash representation of a parsed URI where the keys are the component names and the values are the component values. The shortest form I could come up with using only public methods on `URI::Generic` is rather clumsy-feeling: ```ruby uri = ::URI.parse(url) hsh = [uri.component, uri.select(*uri.component)].transpose.to_h ``` Hence this suggested patch: ``` diff --git a/lib/uri/generic.rb b/lib/uri/generic.rb index cfa0de6b74..f27a07a53c 100644 --- a/lib/uri/generic.rb +++ b/lib/uri/generic.rb @@ -1367,6 +1367,13 @@ def to_s str end + # + # Returns a Hash representing the URI components + # + def to_h + [component, component_ary].transpose.to_h + end + # # Compares two URIs. # ``` Which would allow the much more ergonomic, idiomatic and terse usage: ```ruby uri = ::URI.parse(url) hsh = uri.to_h ``` Also happy to put together tests/specs for that as required. -- https://bugs.ruby-lang.org/ Unsubscribe: