Yeahwheel
Yeahwheel

Reputation: 11

How to concatenate url + string + value ? ruby

Trying to parse through all pages in category of products. I want to check every page using original_url+'&p='+value, where value is number of pages.

How to write this properly full_source = url + "&p=" + "#{number_of_page}" ``

Upvotes: 0

Views: 52

Answers (1)

B Seven
B Seven

Reputation: 45943

I think you want

full_source = "#{url}?p=#{number_of_page}"

Your question had an ampersand:

full_source = "#{url}&p=#{number_of_page}"

A better way to do it would be:

uri = URI.parse(url)
uri.query = URI.encode_www_form({ p: number_of_page })
puts uri.to_s # URL with query params

Here is an example:

url = 'http://google.com/search'
uri = URI.parse(url)
uri.query = URI.encode_www_form({ q: 'ruby' })
puts uri.to_s
# => "http://google.com/search?q=ruby""

Upvotes: 2

Related Questions