Jen Lijó Jen Lijó - 2 months ago 8
Ruby Question

Ruby Array: string into integer

I'm new to Ruby. I have a series of arrays with two strings each:

["[[\"Wayfair \", \"57\"]]", "[[\"Move24 \", \"26\"]]",
"[[\"GetYourGuide \", \"25\"]]", "[[\"Visual Meta \", \"22\"]]",
"[[\"FinLeap \", \"20\"]]", "[[\"Movinga \", \"20\"]]",
"[[\"DCMN \", \"19\"]]", ...


I am trying to convert the string with the number of each array into an integer, but I get something else than I expect:

companies = companies.map do |company|
c = company[0].scan(/(.+)\((\d+)\)/).inspect
[c[0], c[1].to_i]
end


puts:

["[", 0], ["[", 0], ["[", 0], ["[", 0], ["[", 0], ["[", 0],
["[", 0], ["[", 0], ["[", 0], ["[", 0], ["[", 0]]


I am expecting:

["Wayfair", 57], ["Move24", 26], ["GetYourGuide", 25], ...


please help?

Full code :

require 'net/http'
require 'uri'

uri = URI('http://berlinstartupjobs.com/') #URI takes just one url
req = Net::HTTP::Get.new(uri) #get in URI
req['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36' #use this header


res = Net::HTTP.start(uri.hostname, uri.port) {|http| http.request(req)} # URI documentation

puts res.code #status code

puts res.body

puts res.body.scan('<a href="http://berlinstartupjobs.com/companies/') #scan in the body of the document files that match a href=...

puts res.body.scan(/<a href="http:\/\/berlinstartupjobs\.com\/companies\/[^\s]+ class="tag-link">(.*)<\/a>/) #scan

companies = res.body.scan(/<a href="http:\/\/berlinstartupjobs\.com\/companies\/[^\s]+ class="tag-link">(.*)<\/a>/)


companies = companies.map do |company|
c = company[0].scan(/(.+)\((\d+)\)/).inspect
[c[0], c[1].to_i]
end # do ... end = { }

puts companies.inspect

Answer

Your code was mostly ok. Just drop that .inspect at the end. It returns a string, not array.

# this is what you get from the scraping.
companies = [["Wayfair (57)"], ["Move24 (26)"], ["GetYourGuide (25)"]]

companies = companies.flatten.map do |company|
  c = company.scan(/(.+)\((\d+)\)/).flatten
  [c[0], c[1].to_i]
end

p companies
# >> [["Wayfair ", 57], ["Move24 ", 26], ["GetYourGuide ", 25], ...]
Comments