Building a HTML url with parameters from a hash, DRY
Been breaking my nose on this for a good hour. Getting a splat hash argument into a html url to dry things up.
Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!
Been breaking my nose on this for a good hour. Getting a splat hash argument into a html url to dry things up.
Comments
Hi Timon,
Great stuff, just taught me another use for splat!
What do you think of this:
def build_url method, args params = args.map {|k,v| "#{k}=#{v}"}.join('&') url = 'http://your-url.com/'url + method + params end </code>
Sorry, missed the ‘?’ – how’s this:
def build_url method, args params = args.map{|k,v| "#{k}=#{v}"}.join('&')“http://your-url.com/#{method}?#{params}” end </code>
Post a comment