2

I am implementing a REST client in Ruby and am treating base URLs as instances of URI. For the path after the base URL, I am unsure whether to treat it also as a URI instance or as a string.

Approach A

base_url = URI("http://www.foo.com")
path = URI("/someaction")

Approach B

base_url = URI("http://www.foo.com")
path = "/someaction"

With both of the above approaches, I plan to call URI.join(base_url, path) before making my request. Which of the approaches would be considered a better practice?

Jonathan
  • 123
  • 2
  • 9
  • 1
    I wouldn't use URI for URLs only. try `URI("uri:wtf")` and look [this answer](http://stackoverflow.com/a/16359999/413494) – fotanus Jun 07 '13 at 16:51
  • Good to know, @fotanus. I'm surprised Ruby doesn't provide better built-in support for URLs. – Jonathan Jul 03 '13 at 22:15

1 Answers1

1

You're worrying about something not worth worrying about. Let URI do what it's good at and designed to do:

base_url = URI("http://www.foo.com")
base_url.path = "/someaction"

base_url
=> #<URI::HTTP:0x00000102079d58 URL:http://www.foo.com/someaction>

Move along to something else.

If you need to manipulate a path that has been extracted from a URL, look at split, basename, extname and dirname from the File class. They do it in a nice standardized manner.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303