I want to be able to store variables into an array and then loop through them then store the results in an array and return that new array.
The code should explain what I'm trying to do better:
This is my attempt:
# Important initializations
url_check_array = Array.new
return_array = Array.new
# The following initializes classes which then does the check by using the two variables that's passed to it
check_overall = CheckString.new("#{gitlab_cache}", 'success')
check_cache = CheckString.new("#{gitlab_cache}", 'success')
check_database = CheckString.new("#{gitlab_database}", 'success')
check_migrations = CheckString.new("#{gitlab_migrations}", 'success')
unless @is_verbose == "true"
# I haven't done much here as I am not done yet (still trying to get things work on the 'else' part)
url_check_array.insert(check_overall)
puts "url_check_array = #{check_overall}" # DEBUG
puts "Gitlab OVERALL URL is: #{gitlab_overall}" # DEBUG
else
# url_check_array.insert("#{check_cache}", "#{check_database}", "#{check_migrations}") # Results in TypeError - no implicit conversion of String into Integer
url_check_array.insert(check_cache, check_database, check_migrations)
url_check_array.each do |check_item|
return_array.insert(check_item)
end
#puts "Result is: #{check_cache.class}" # DEBUG
return return_array
end
I cannot figure out what I'm doing wrong as the above code results in errors.
EDIT: So the question is.. How do I successfully put those class initializations in an array and then loop through them properly and then put the results in an another array?
EDIT2: Right now I'm getting the following error(s):
TypeError - no implicit conversion of CheckString into Integer
EDIT3: So, I was able to add those to the array by changing url_check_array.insert to url_check_array.push... However, my problem now is that the loop that I have doesn't do much except put the same thing into the return_array (which is the following):
[#<CheckString:0x007ff51e0b5278 @uri_str="https://git.company.com/health_check.json?token=eeMGuv", @pattern="success">, #<CheckString:0x007ff51e0b51b0 @uri_str="https://git.company.com/health_check.json?token=eeMGuv", @pattern="success">, #<CheckString:0x007ff51e0b5110 @uri_str="https://git.company.com/health_check.json?token=eeMGuv", @pattern="success">]
Instead of the actual results.. So, what I really want it to do is actually execute the CheckString.new("#{gitlab_migrations}", 'success') and then put the result into an array.
Array#insertis expected to be an integer index, but you're passing it a CheckString object, hence the TypeError.eachpushing the element into a separate array. This is fine but it's basically the same thing asmap. If you're having problems with your code, show an error or at least say how your actual result is incorrect.