Parsing JSON from text file - ruby

I have a log file that appears to have a time stamp followed by a tab, then a string of JSON.
10/28/2014 00:04:51 {"servers":[{"id":833495,"account_id":39033,"name":"USTSMASCMSP113","host":"USTSMASCMSP113","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":28.7,"cpu_stolen":0,"disk_io":0.17,"memory":60.8,"memory_used":10444865536,"memory_total":17176723456,"fullest_disk":84.0,"fullest_disk_free":8171000000},"links":{"alert_policy":28370}},{"id":831246,"account_id":39033,"name":"USTSMASCMSP118","host":"USTSMASCMSP118","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":30.7,"cpu_stolen":0,"disk_io":2.57,"memory":54.3,"memory_used":9329180672,"memory_total":17176723456,"fullest_disk":83.1,"fullest_disk_free":8653000000},"links":{"alert_policy":28370}},{"id":833455,"account_id":39033,"name":"USTSMASCMSP119","host":"USTSMASCMSP119","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":36.3,"cpu_stolen":0,"disk_io":0.38,"memory":54.5,"memory_used":9362735104,"memory_total":17176723456,"fullest_disk":73.7,"fullest_disk_free":17912000000},"links":{"alert_policy":28370}},{"id":838342,"account_id":39033,"name":"USTSMASCMSP120","host":"USTSMASCMSP120","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":29.6,"cpu_stolen":0,"disk_io":0,"memory":55.6,"memory_used":9550430208,"memory_total":17176723456,"fullest_disk":84.9,"fullest_disk_free":10259000000},"links":{"alert_policy":28370}},{"id":833500,"account_id":39033,"name":"USTSMASCMSP121","host":"USTSMASCMSP121","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":38.9,"cpu_stolen":0,"disk_io":5.47,"memory":56.7,"memory_used":9743368192,"memory_total":17176723456,"fullest_disk":84.9,"fullest_disk_free":10288000000},"links":{"alert_policy":28370}},{"id":831221,"account_id":39033,"name":"USTSMASCMSP123","host":"USTSMASCMSP123","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":32.4,"cpu_stolen":0,"disk_io":0.42,"memory":53.8,"memory_used":9232711680,"memory_total":17176723456,"fullest_disk":87.2,"fullest_disk_free":8705000000},"links":{"alert_policy":28370}},{"id":833466,"account_id":39033,"name":"USTSMASCMSP124","host":"USTSMASCMSP124","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":25.1,"cpu_stolen":0,"disk_io":4.28,"memory":58.6,"memory_used":10065281024,"memory_total":17176723456,"fullest_disk":74.2,"fullest_disk_free":19387000000},"links":{"alert_policy":28370}},{"id":838346,"account_id":39033,"name":"USTSMASCMSP125","host":"USTSMASCMSP125","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":32.2,"cpu_stolen":0,"disk_io":0.13,"memory":56.5,"memory_used":9703522304,"memory_total":17176723456,"fullest_disk":89.1,"fullest_disk_free":7443000000},"links":{"alert_policy":28370}},{"id":833504,"account_id":39033,"name":"USTSMASCMSP126","host":"USTSMASCMSP126","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":29.6,"cpu_stolen":0,"disk_io":0,"memory":56.9,"memory_used":9776922624,"memory_total":17176723456,"fullest_disk":85.7,"fullest_disk_free":9761000000},"links":{"alert_policy":28370}},{"id":831212,"account_id":39033,"name":"USTSMASCMSP127","host":"USTSMASCMSP127","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":30.5,"cpu_stolen":0,"disk_io":0,"memory":50.9,"memory_used":8735686656,"memory_total":17176723456,"fullest_disk":85.0,"fullest_disk_free":10222000000},"links":{"alert_policy":28370}},{"id":833510,"account_id":39033,"name":"USTSMASCMSP128","host":"USTSMASCMSP128","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":26.8,"cpu_stolen":0,"disk_io":0,"memory":57.8,"memory_used":9936306176,"memory_total":17176723456,"fullest_disk":79.7,"fullest_disk_free":10393000000},"links":{"alert_policy":28370}},{"id":833473,"account_id":39033,"name":"USTSMASCMSP129","host":"USTSMASCMSP129","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":31.9,"cpu_stolen":0,"disk_io":0.17,"memory":56.4,"memory_used":9693036544,"memory_total":17176723456,"fullest_disk":86.7,"fullest_disk_free":9095000000},"links":{"alert_policy":28370}},{"id":3922351,"account_id":39033,"name":"USTSMASCMSP136","host":"USTSMASCMSP136","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":16.2,"cpu_stolen":0,"disk_io":0.28,"memory":13.3,"memory_used":4552916992,"memory_total":34356592640,"fullest_disk":73.3,"fullest_disk_free":56043000000},"links":{"alert_policy":28369}},{"id":831184,"account_id":39033,"name":"USTSMASCMSP144","host":"USTSMASCMSP144","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":30.8,"cpu_stolen":0,"disk_io":0.88,"memory":61.8,"memory_used":10617880576,"memory_total":17176723456,"fullest_disk":90.9,"fullest_disk_free":8237000000},"links":{"alert_policy":28370}},{"id":833478,"account_id":39033,"name":"USTSMASCMSP145","host":"USTSMASCMSP145","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":30.5,"cpu_stolen":0,"disk_io":0,"memory":50.3,"memory_used":8631877632,"memory_total":17176723456,"fullest_disk":81.5,"fullest_disk_free":9712000000},"links":{"alert_policy":28370}},{"id":838357,"account_id":39033,"name":"USTSMASCMSP146","host":"USTSMASCMSP146","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":29.7,"cpu_stolen":0,"disk_io":0,"memory":52.8,"memory_used":9069133824,"memory_total":17176723456,"fullest_disk":80.6,"fullest_disk_free":9934000000},"links":{"alert_policy":28370}},{"id":838360,"account_id":39033,"name":"USTSMASCMSP147","host":"USTSMASCMSP147","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":28.7,"cpu_stolen":0,"disk_io":0,"memory":73.6,"memory_used":12640583680,"memory_total":17176723456,"fullest_disk":80.8,"fullest_disk_free":9825000000},"links":{"alert_policy":28370}},{"id":3964940,"account_id":39033,"name":"USTSMASCMSP148","host":"USTSMASCMSP148","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":12.6,"cpu_stolen":0,"disk_io":0.08,"memory":10.6,"memory_used":3636461568,"memory_total":34356592640,"fullest_disk":73.2,"fullest_disk_free":56213000000},"links":{"alert_policy":28369}},{"id":832099,"account_id":39033,"name":"USTSMASCMSP196","host":"USTSMASCMSP196","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":24.8,"cpu_stolen":0,"disk_io":0,"memory":30.2,"memory_used":5187305472,"memory_total":17176723456,"fullest_disk":77.2,"fullest_disk_free":11962000000},"links":{"alert_policy":28370}},{"id":832093,"account_id":39033,"name":"USTSMASCMSP197","host":"USTSMASCMSP197","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":24.8,"cpu_stolen":0,"disk_io":0.34,"memory":30.1,"memory_used":5169479680,"memory_total":17176723456,"fullest_disk":74.5,"fullest_disk_free":13362000000},"links":{"alert_policy":28370}},{"id":832082,"account_id":39033,"name":"USTSMASCMSP198","host":"USTSMASCMSP198","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":23.5,"cpu_stolen":0,"disk_io":0,"memory":26.8,"memory_used":4604297216,"memory_total":17176723456,"fullest_disk":72.8,"fullest_disk_free":14268000000},"links":{"alert_policy":28370}},{"id":832077,"account_id":39033,"name":"USTSMASCMSP199","host":"USTSMASCMSP199","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":21.0,"cpu_stolen":0,"disk_io":0,"memory":31.3,"memory_used":5379194880,"memory_total":17176723456,"fullest_disk":71.5,"fullest_disk_free":14950000000},"links":{"alert_policy":28370}},{"id":832832,"account_id":39033,"name":"USTSMASCMSP200","host":"USTSMASCMSP200","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":50.3,"cpu_stolen":0,"disk_io":0,"memory":28.9,"memory_used":4966055936,"memory_total":17176723456,"fullest_disk":74.3,"fullest_disk_free":13477000000},"links":{"alert_policy":28370}},{"id":831194,"account_id":39033,"name":"USTSMASCMSP201","host":"USTSMASCMSP201","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":27.0,"cpu_stolen":0,"disk_io":0,"memory":28.6,"memory_used":4918870016,"memory_total":17176723456,"fullest_disk":74.4,"fullest_disk_free":13397000000},"links":{"alert_policy":28370}},{"id":832059,"account_id":39033,"name":"USTSMASCMSP202","host":"USTSMASCMSP202","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":25.1,"cpu_stolen":0,"disk_io":0,"memory":26.6,"memory_used":4564451328,"memory_total":17176723456,"fullest_disk":68.3,"fullest_disk_free":16645000000},"links":{"alert_policy":28370}},{"id":832043,"account_id":39033,"name":"USTSMASCMSP203","host":"USTSMASCMSP203","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":24.7,"cpu_stolen":0,"disk_io":1.17,"memory":25.2,"memory_used":4326424576,"memory_total":17176723456,"fullest_disk":68.2,"fullest_disk_free":16646000000},"links":{"alert_policy":28370}},{"id":832034,"account_id":39033,"name":"USTSMASCMSP204","host":"USTSMASCMSP204","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":26.5,"cpu_stolen":0,"disk_io":0,"memory":27.1,"memory_used":4653580288,"memory_total":17176723456,"fullest_disk":68.5,"fullest_disk_free":16510000000},"links":{"alert_policy":28370}},{"id":832031,"account_id":39033,"name":"USTSMASCMSP205","host":"USTSMASCMSP205","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":27.8,"cpu_stolen":0,"disk_io":0,"memory":24.9,"memory_used":4285530112,"memory_total":17176723456,"fullest_disk":68.8,"fullest_disk_free":16376000000},"links":{"alert_policy":28370}},{"id":832024,"account_id":39033,"name":"USTSMASCMSP206","host":"USTSMASCMSP206","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":30.2,"cpu_stolen":0,"disk_io":0,"memory":26.5,"memory_used":4548722688,"memory_total":17176723456,"fullest_disk":67.2,"fullest_disk_free":17181000000},"links":{"alert_policy":28370}},{"id":831202,"account_id":39033,"name":"USTSMASCMSP207","host":"USTSMASCMSP207","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":23.9,"cpu_stolen":0,"disk_io":0,"memory":25.6,"memory_used":4394582016,"memory_total":17176723456,"fullest_disk":67.3,"fullest_disk_free":17146000000},"links":{"alert_policy":28370}},{"id":3979556,"account_id":39033,"name":"USTSMASCMSP227","host":"USTSMASCMSP227","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":8.25,"cpu_stolen":0,"disk_io":5.96,"memory":18.1,"memory_used":3116367872,"memory_total":17174626304,"fullest_disk":65.1,"fullest_disk_free":18301000000},"links":{"alert_policy":28369}},{"id":3965125,"account_id":39033,"name":"USTSMASCMSP247","host":"USTSMASCMSP247","health_status":"green","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":18.2,"cpu_stolen":0,"disk_io":34.0,"memory":32.3,"memory_used":5543821312,"memory_total":17174626304,"fullest_disk":59.9,"fullest_disk_free":28765000000},"links":{"alert_policy":28369}},{"id":3965022,"account_id":39033,"name":"USTSMASCMSP248","host":"USTSMASCMSP248","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":43.9,"cpu_stolen":0,"disk_io":8.61,"memory":28.1,"memory_used":4821352448,"memory_total":17143169024,"fullest_disk":86.5,"fullest_disk_free":9645000000},"links":{"alert_policy":28369}},{"id":3851100,"account_id":39033,"name":"ustsmvscmsp888","host":"ustsmvscmsp888","health_status":"orange","reporting":true,"last_reported_at":"2014-10-28T04:04:17+00:00","summary":{"cpu":19.7,"cpu_stolen":0,"disk_io":0,"memory":42.9,"memory_used":1841299456,"memory_total":4293918720,"fullest_disk":72.9,"fullest_disk_free":14243000000},"links":{"alert_policy":28369}}],"links":{"server.alert_policy":"/v2/alert_policies/{alert_policy_id}"}}
I have code that reads the input file and I am attempting to get values from the string like so.
require 'json'
input = File.open("input.txt", 'r')
input.each do |line|
line = line.split("\t")
my_json = line[1].to_json
puts my_json["cpu"]
end
The end result looks like this:
C:\>ruby test.rb
cpu
I am not entirely sure if the string itself is not formatted correctly or if I'm not calling on the value correctly.

Run this code inside console and check the return but i'm pretty sure you can't call
my_json["cpu"]
You will need to call something like:
my_json["servers"].first["cpu"]

my_json = line[1].to_json
Will take an object and convert it to JSON, not parse an existing JSON string into a ruby object. You're going to wind up with an escaped string within a JSON string.
You need something like
my_json = JSON.parse(line[1])
you can verify this has worked correctly by outputting my_json to stdout:
$stdout << my_json.class.name
You should have an instance of Hash or Openstruct.

Consider this:
require 'json'
line = '10/28/2014 00:04:51 {"servers":[{"id":833495,"account_id":39033}]}'
JSON[line[/\{.+}/]]
# => {"servers"=>[{"id"=>833495, "account_id"=>39033}]}
That means you can reduce your parsing code to something like:
File.foreach("input.txt") do |line|
data = JSON[line[/\{.+}/]]
# do something with the data
end
Regular expressions are greedy, so /\{.+}/ will start at the first { and look until it finds the last }, and return the starting and ending braces, and everything in between.
The JSON [] method is smart enough to know that if its parameter is a string it should try to parse it and return a Ruby array or hash. If the parameter is an Array or Hash, it will serialize it.
Note: It isn't necessary, and definitely not desirable to include huge data samples like you did. Reduce it to the bare minimum necessary to demonstrate the problem.

Related

Variable does not equal variable when it should in ruby

I am making my first web server, but When i use the if statement to compare user input from html file with a string, they just skip it even if it is true.
require 'socket'
require 'pry'
class Parser
def parse(req, clients)
save = File.new("requests.txt", "w+")
save.write(req.chomp)
save.close
words = IO.readlines("requests.txt").last(1)
if words == "Username=test&Password=1234"
success(clients)
end
#binding.pry
puts words
end
end
def success(succ_client)
success_file = File.read("templates/success.html")
stuff = "HTTP/1.1 200\r\n" + success_file.force_encoding('UTF-8')
succ_client.puts(stuff)
end
def response(cli)
file = File.read("templates/passpage.html")
content = "HTTP/1.1 200\r\n" + file.force_encoding('UTF-8')
cli.puts(content)
end
serv_sock = TCPServer.new('10.0.2.15', 8080)
loop {
client = serv_sock.accept()
requests = Parser.new
requests.parse(client.readpartial(2043), client)
response(client)
client.close
puts "Connected"
}
I tried using #compact!, nil?, and using pry to decode to find whats the issue, but I just cant find the problem, when i puts the words variable it puts the correct value but its just not the right one I guess. I tried decoding the words but that still didn't work unless i did it wrong.
It has been 5 days on this problem and this is my first ruby program, and web-server, So ill appreciate any help I can get with this to move forward in life.
There are several issues with your code.
Primarily, your call to IO.readlines("requests.txt").last(1) returns an Array of zero or one strings. However, you are comparing it to a single string. Since the string is not equal to the array, the comparison fails.
Likely, you want to use IO.readlines("requests.txt").last instead. This returns the last element or the array or nil if the array returned by the readlines method is empty.
Please have a look at the documentation of the Array#last method to learn more about the returned types of this method.
In addition to that, even if your if statement eventually matches, your intended call to the success method fill fail, because you have defined it in the global main object, rather than your Parser class.
Also, when you eventually call success from your parse method, you will be returning two responses as you also call the response method later. You may want to rethink the way you select the correct responses...

Store Ruby YAML results into an array

So I have an empty array and a .yml file. I have managed to output the results of that file with this code
puts YAML.load_file('some_yml_file.yml').inspect
I was wondering, how can I pull out each of the data and store them into an empty array?
Is it
emptyarray = []
YAML.load_file('some_yml_file.yml').inspect do |entry|
emptyarray << entry
end
Any help would be appreciated! Thanks!
YAML.load_file returns a Ruby object corresponding to the type of data structure the YAML represents. If the YAML contains a sequence, YAML.load_file will return a Ruby array. You don't need to do anything further to put the data into an array, because it's already an array:
yaml = <<END
---
- I am
- a YAML
- sequence
END
data = YAML.load(yaml)
puts data.class
# => Array
puts data == ["I am", "a YAML", "sequence"]
# => true
(You'll notice that I used YAML.load to load the data from a string rather than a file, but the result is the same as using YAML.load_file on a file with the same contents.)
If the top-level structure in the YAML is not a sequence (e.g. if it's a mapping, analogous to a Ruby hash), then you will have to do additional work to turn it into an array, but we can't tell you what that code would look like without seeing your YAML.
Change YAML.load_file('some_yml_file.yml').inspect do |entry| with YAML.load_file('some_yml_file.yml').each do |entry| and it should work as you expect it (assuming it's not a string).
If you post a sample of your data structure inside the YAML file and what you wish to extract and put in an array then that would help.

Performing same action for each value in array in Ruby

I have a text file of stock symbols, each symbol is on its own line. In Ruby, I have created an array from the text file like so:
symbols = []
File.read('symbols.txt').each_line do |line|
symbols << line.chop!
end
For each symbol in the array, I want to read from a json file (ex. MSFT.json) and perform a number of calculations (all of that is now working) and then do the same thing for the next symbol in the array.
When attempting to "call" and perform calculations on the first item in the array I did this:
json = File.read("#{symbols[0]}.json")
#...calculations come after this
This worked fine, and it did run through the whole program for the first symbol, but of course doesn't go on to perform the same steps for the remaining symbols (I know thats because I specified an index in the array].
Now that I know that the program works for a single symbol, I now want it to run on all the symbols in the array...so after the first block, I tried adding: symbols.each do, and removed the [0] from the File.read line (and added end at the end of the calculations). I was hoping it would loop through everything between the "do" and "end" for each symbol. That didn't work.
Then I tried adding this after the first block:
def page(symbols, i)
page[i]
end
And changing the File.read line to: json = File.read("#{page[i]}.json)
But that didn't work either.
Any help is appreciated. Thanks a lot
You can simply use .each instead of an iterator index:
symbols.each do |symbol|
json = File.read("#{symbol}.json")
# do some calculation for symbol
end
No need to iterate twice:
open('symbols.txt').lines.each do |line|
symbol = line.strip
json = File.read("#{symbol}.json")
# process json
end

A string (thats looks like an array) into an array Ruby

I have an output from an API that look like this... (its a string)
[[2121212,212121,asd],[2323232,23232323,qasdasd]]
Its a string - not an array. I want to convert it to an array and then extract the first two elements in each array in the nested array to:
[2121212,212121],[2323232,23232323]
What's the best way to do this ruby? I could use regexp and extract - but basically the string is already an array, however the class is a string.
I tried
array.push(response)
but that just put the string in to the array as one element. I guess what would be nice is a to_array method
You will need to use regular expression anyway if not eval (shrudder...), this is the shortest one
str = "[[2121212,212121,asd],[2323232,23232323,qasdasd],[2424242,24242424,qasdasd]]"
p str.scan(/(\d+),(\d+)/)
=>[["2121212", "212121"], ["2323232", "23232323"], ["2424242", "24242424"]]
Assuming this is a JSON response (and if so, it is badly malformed and you should talk to the people that are responsible for this) you could write something like:
require 'json'
input= '[[2121212,212121,Asd],[2323232,23232323,qasdasd]]'
input.gsub!(/([A-Za-z ]+)/,'"\1"')
json = JSON.parse input
output = json.map{|x| x[0...2]}
p output
this prints
[[2121212, 212121], [2323232, 23232323]]
Using eval is very bad but I have no other easy option.
test_str = "[[2121212,212121,asd],[2323232,23232323,qasdasd]]"
test_str.gsub!(/([a-z]+)/) do
"'#{$1}'"
end
=> "[[2121212,212121,'asd'],[2323232,23232323,'qasdasd']]"
test_array = eval(test_str)
=> [[2121212, 212121, "asd"], [2323232, 23232323, "qasdasd"]]
test_array.each do |element|
element.delete(element.last)
end
=> [[2121212, 212121], [2323232, 23232323]]

Why does Array.to_s return brackets?

For an array, when I type:
puts array[0]
==> text
Yet when I type
puts array[0].to_s
==> ["text"]
Why the brackets and quotes? What am I missing?
ADDENDUM: my code looks like this
page = open(url) {|f| f.read }
page_array = page.scan(/regex/) #pulls partial urls into an array
partial_url = page_array[0].to_s
full_url = base_url + partial_url #adds each partial url to a consistent base_url
puts full_url
what I'm getting looks like:
http://www.stackoverflow/["questions"]
This print the array as is without brackets
array.join(", ")
to_s is just an alias to inspect for the Array class.
Not that this means a lot other than instead of expecting array.to_s to return a string it's actually returning array.inspect which, based on the name of the method, isn't really what you are looking for.
If you want just the "guts" try:
page_array.join
If there are multiple elements to the array:
page_array.join(" ")
This will make:
page_array = ["test","this","function"]
return:
"test this function"
What "to_s" on an Array returns, depends on the version of Ruby you are using as mentioned above. 1.9.X returns:
"[\"test\"]"
You need to show us the regex to really fix this properly, but this will do it:
Replace this
partial_url = page_array[0].to_s
with this
partial_url = page_array[0][0]
This doesn't necessarily fix why you are getting a doubled-up array, but you can flatten it and then call the first element like this.
page_array = page.scan(/regex/).flatten
Flattening takes out stacked arrays and creates one level, so if you had [1,2,[3,[4,5,6]]] and called flatten on it, you would get [1,2,3,4,5,6]
It is also more robust than doing array[0][0], because, if you had more than two arrays nested in the first element, you would run into the same issue.
Iain is correct though, without seeing the regex, we can't suss out the root cause.

Resources