Ich bin neu in Ruby. Ich habe eine Reihe von Arrays mit zwei Saiten jeweils:Ruby Array: String in ganze Zahl
["[[\"Wayfair \", \"57\"]]", "[[\"Move24 \", \"26\"]]",
"[[\"GetYourGuide \", \"25\"]]", "[[\"Visual Meta \", \"22\"]]",
"[[\"FinLeap \", \"20\"]]", "[[\"Movinga \", \"20\"]]",
"[[\"DCMN \", \"19\"]]", ...
Ich versuche, die Zeichenfolge mit der Anzahl der jedes Array in eine ganze Zahl zu konvertieren, aber ich habe etwas anderes, als ich erwarten:
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]]
ich erwarte:
["Wayfair", 57], ["Move24", 26], ["GetYourGuide", 25], ...
bitte helfen?
Voll 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
Können Sie die erwartete Ausgabe auch anzeigen? –
So wie ich es sehe, haben Sie eine flache Reihe von Saiten. –
Yeh, wäre schön, die erwartete Ausgabe zu sehen – Phil