[prev in list] [next in list] [prev in thread] [next in thread]
List: ruby-core
Subject: [ruby-core:108142] [Ruby master Feature#18594] Add a #to_h method on URI::Generic
From: "jimcavoli (Jim Cavoli)" <noreply () ruby-lang ! org>
Date: 2022-03-31 13:18:35
Message-ID: redmine.journal-97118.20220331131834.52183 () ruby-lang ! org
[Download RAW message or body]
Issue #18594 has been updated by jimcavoli (Jim Cavoli).
The use case I had that brought this up was actually around generating complex \
configuration files for another product from a simplified YAML config file, which \
involved extracting selected URI components to create a tokenized version for naming \
entities in the generated files. One key feature was the ability to override certain \
components used for the tokenizing process, and merging (compacted) hash \
representations was the simplest approach in that case to calculate the final values. \
Ultimately, it was not that big of a deal, as written above, to transpose an array of \
the components themselves, but just seemed like a path of more surprise than expected \
from a stdlib component.
----------------------------------------
Feature #18594: Add a #to_h method on URI::Generic
https://bugs.ruby-lang.org/issues/18594#change-97118
* 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: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic