0

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
7
  • 1
    Can you show the expected output too? Commented Oct 4, 2016 at 7:57
  • 1
    The way I see it, you have a flat array of strings. Commented Oct 4, 2016 at 7:57
  • Yeh, would be nice to see expected output Commented Oct 4, 2016 at 7:58
  • Please, provide a minimal reproducible example. Commented Oct 4, 2016 at 7:59
  • I've updated the question with full info. Commented Oct 4, 2016 at 8:04

3 Answers 3

1

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], ...]
Sign up to request clarification or add additional context in comments.

1 Comment

hey Sergio thank you so much - this is what I needed. I had a feeling flatten was the quid and been playing with it, but not this way. Thanks again :)
1

You can achieve this by using Enumerable#map & parsing each element using JSON.parse:

require 'json'

companies.map { |elem| key, val = JSON.parse(elem).flatten; [k.strip, v.to_i] }

Instead of JSON.parse you also can use eval, but using eval is considered to be a bad practice.

1 Comment

nah, his data is not like this. OP just messed up the printing.
1
arr = ["[[\"Wayfair \", \"57\"]]", "[[\"Move24 \", \"26\"]]"]
result = arr.collect{|e| JSON.parse(e)[0].map{|name, value| [name.strip, value.to_i]}}

OUTPUT:
[[Wayfair, 57], [Move24", 26]]

1 Comment

nah, his data is not like this. OP just messed up the printing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.