Performing same action for each value in array in Ruby - 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

Related

Outputting hash to text file

I am having trouble outputting the contents of my hash to a file. The program is one that manages a list of student records, including their StudentID, first name, last name, Major, and catalog year. Once the user is finished adding records, it is then added to the hash.
Everything in the program works perfectly, except when I try running the quit_program function, it doesn't save the contents in the file. Additionally, i am not getting any errors, any ideas?
could it potentially not be working because it is having trouble with converting the text in my hash, which is alphanumeric, into the text file?
def quit_program()
puts "Save Changes? y/n"
#changes = gets().chomp
if #changes=="y"
#fh=File.open(#file_name, 'w')
#this_string=""
#sDB.each do |key, store_account_data| #line 50
puts "#{key}: #{store_account_data.join(',')}"
end
end
#fh.puts(#this_string)
#fh.close()
end
You're not writing anything to the file. The string #this_string is empty. You should do
#sDB.each do |key, store_account_data|
#fh.puts "#{key}: #{store_account_data.join(',')}"
end
it doesn't save the contents in the file.
The following is NOT how you write to a file:
puts "#{key}: #{store_account_data.join(',')}"
That is how you write to your terminal/console window.
And this code:
#this_string=""
#fh.puts(#this_string)
writes a blank string to the file.
Here is how you write to a file:
class Student
def initialize(sDB, filename)
#sDB = sDB
#filename = filename
end
def save_changes()
puts "Save Changes? y/n"
user_answer = gets().chomp
if user_answer == "y"
File.open(#file_name, 'w') do |f|
#sDB.each do |key, store_account_data| #line 50
f.puts "#{key}: #{store_account_data.join(',')}"
end
end
end
end
could it potentially not be working because it is having trouble with
converting the text in my hash, which is alphanumeric, into the text
file?
No. Here is a concrete example you can try:
data = {
"John" => ['a', 123, 'b', 456],
"Sally" => ['c', 789, 'b', 0]
}
File.open('data.txt', 'w') do |f|
data.each do |name, data|
f.puts "#{name}: #{data.join(',')}"
end
end
$ ruby myprog.rb
$ cat data.txt
John: a,123,b,456
Sally: c,789,b,0
Also, ruby indenting is 2 spaces--not 0 spaces or 3 spaces, or anything else.
The answer is given in the error message: undefined local variable or method 'sDB'. (Which you have since removed from your question making the edited version next to impossible to answer.) Where and when is sDB defined in your program? You are evidently attempting to quit before initializing it.
In any case it is not a good thing to be accessing instance variables directly inside other methods. You should use accessor (getter and setter) methods instead. That would have probably prevented this situation from biting you in the first place.
def sdb
#sDB ||= Hash.new
end
def sdb=( key, value )
sdb
#sDB[ key ] = value
end
. . .
You are not properly writing to a file even if #sDB is defined. See Ruby - Printing a hash to txt file for an example.
Your question is missing essential input data, so there's no way to test our suggested changes.
Here's untested code I'd work from:
def quit_program
puts "Save Changes? y/n"
if gets.chomp.downcase == 'y'
File.write(
#file_name,
#s_db.map{ |k, v| "#{ k }: #{ v.join(',') }" }.join("\n")
)
end
end
Note:
#sDB isn't a proper variable name in Ruby. We use snake_case, not camelCase for variables and method names. ItsAMatterOfReadability. Follow the convention or suffer the wrath of your team members the first time you have a code review.
Don't add empty parenthesis to method names (quit_program()) or calls (gets()) unless it's essential to tell the difference between a variable and a method invocation. You should also never name a variable the same as a method because it'll confuse everyone working on the code, so that should never be a consideration.
Don't create a variable (#changes) you use once and throw away, unless what you're doing is so complex you need to break down the operation into smaller chunks. And, if you're doing that, it'd be a really good candidate for refactoring into separate methods, so again, just don't.
When comparing user-input to something you expect, fold the case of their input to match what you expect. (gets.chomp.downcase == 'y'). It really irritates users to enter "y" and fail because you insisted on "Y".
While you can use File.open to create or write to a file, there's less visual noise to use File.write. open is great when you need to use various options for the mode but for plain text write is sufficient.
The whole block used for writing looks like it can be cleaned up to a single map and join, which coerces the data into an array of strings then into a single string.

Parsing JSON from text file

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.

Triple-loop Ruby

I know the idea of a triple loop brings fear to the minds of some, but I have a code with the following structure:
paragraph.split(/(\.|\?|\!)[\s\Z]/).each do |sentence|
myArrayOfFiles.each_with_index { |ma,j|
ma.each_with_index { |word,i|
sentence.gsub!(...)
}
}
end
The two outer loops run as expected, but for some reason, the inner loop runs over the first sentence only. Do you know why this is? How can I make the inner loop run over all sentences too?
I am running on Ruby 1.8.7, and have tried the same code above using just the each loop and got the same results. Any ideas?
EDIT:
myArrayOfFiles is an array filled by:
AFile = File.open("A.txt")
BFile = File.open("B.txt")
myArrayOfFiles << [Afile,BFile]
myArrayOfFiles.flatten!
Your problem is that myArrayOfFiles contains File instances. When you iterate through one of your Files with ma.each_with_index, it will go through the file line by line and stop at EOF. Then, you try to iterate again with the next sentence but the File is already at EOF so ma.each_with_index has nothing to iterate over and nothing interesting happens. You need to call rewind to move the Files back to the beginning before you try to each_with_index them again:
paragraph.split(/(\.|\?|\!)[\s\Z]/).each do |sentence|
myArrayOfFiles.each_with_index do |ma, j|
ma.rewind # <------------------------- You need this
ma.each_with_index do |word, i|
sentence.gsub!(...)
end
end
end

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.

ruby simple substitutions

very new to Ruby, I've got the following situation. I have a file with values separated by new lines, they look like this:
18917
18927
18929
...
I want to prepend a folder path to all of them, then grab the first 2 characters and prepend that as well, then the value in the file and then append a '.jpg' at the end so they would end up looking like this:
path/to/foler/18/18917.jpg
So I've code this ruby code:
folder = "/path/to/folder"
lines = File.readlines("values.csv")
images = lines.collect.to_s.gsub("\n", ".jpg,")
images.split(',').collect { |dogtag | puts "bad dog: #{folder}/#{dogtag[0,2]}/#{dogtag}" }
Now, this almost works, the part that is not working is the grabbing of the first 2 characters. I also tried it with the method outside quotes (and without the #{} of course) but it just produces an empty result.
Could someone please point out my folly?
Eventually I want to delete those images but I'm guessing that substituting 'File.delete' for 'puts' above would do the trick?
As usual, thanks in advance for taking the time to look at this.
You don't seem to be understanding what collect does.
I would rewrite your snippet like this:
File.read("values.csv").each do |line|
puts "bad dog: /path/to/folder/#{line[0,2]}/#{line.chomp}.jpg"
end
-- Update for last comment: --
If you don't want to use an if statement to check if a file exists before deleting it, you have two option (AFAIK).
Use rescue:
File.read("values.csv").each do |line|
begin
File.delete "/path/to/folder/#{line[0,2]}/#{line.chomp}.jpg"
rescue Errno::ENOENT
next
end
end
Or extend the File class with a delete_if_exists method. You can just put this at the top of your script:
class File
def self.delete_if_exists(f)
if exists?(f)
delete(f)
end
end
end
With this, you can do:
File.read("values.csv").each do |line|
File.delete_if_exists "/path/to/folder/#{line[0,2]}/#{line.chomp}.jpg"
end

Resources