ruby / uri

URI is a module providing classes to handle Uniform Resource Identifiers
https://ruby.github.io/uri/
Other
79 stars 42 forks source link

Add URI::Generic#deconstruct and URI::Generic#deconstruct_keys #56

Open miyucy opened 1 year ago

miyucy commented 1 year ago

I added the following method because there are times when I want to right-assign the components of a URI object or use pattern matching while writing an application.

irb(main):003:0> u = URI("http://example.com")
=> #<URI::HTTP http://example.com>
irb(main):004:0> u => { host: }
(irb):4:in `<main>': #<URI::HTTP http://example.com>: #<URI::HTTP http://example.com> does not respond to #deconstruct_keys (NoMatchingPatternError)
        from ~/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/irb-1.6.2/exe/irb:11:in `<top (required)>'
        from ~/.rbenv/versions/3.2.1/bin/irb:25:in `load'
        from ~/.rbenv/versions/3.2.1/bin/irb:25:in `<main>'

irb(main):002:0> u = URI("http://example.com")
=> #<URI::HTTP http://example.com>
irb(main):003:1* case u
irb(main):004:1* in host: /foo.com/
irb(main):005:1*   puts "OK"
irb(main):006:1* else
irb(main):007:1*   puts "NG"
irb(main):008:0> end
NG
=> nil