1

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.

8
  • Do you have a question? Is there a specific problem you're having? Are you getting an error message or a different result than you expect that you forgot to include in your question? Commented Oct 6, 2016 at 16:57
  • I'll update OP. Commented Oct 6, 2016 at 16:58
  • 1
    The first argument to Array#insert is expected to be an integer index, but you're passing it a CheckString object, hence the TypeError. Commented Oct 6, 2016 at 16:59
  • Yeah, I noticed that.. But as the updated OP says, how do I successfully put those class initializations into an array and loop through them to execute them one by one then put the results of each of those into an another array to return it to the main app? Commented Oct 6, 2016 at 17:03
  • 2
    you have each iteration of each pushing the element into a separate array. This is fine but it's basically the same thing as map. If you're having problems with your code, show an error or at least say how your actual result is incorrect. Commented Oct 6, 2016 at 17:35

1 Answer 1

1

You put the classes themself in your result array. Your purpose is to put the results itself in that array?

So in your class CheckString you schould have a method 'check' or something like that that puts out the result. Then in your mapping you should call thatr check method.

url_check_array.each do |check_item|
  return_array.insert(check_item.check)
end

class CheckString
  def check
    # do some checking and return something that indicates a succesful check or not, eg
    @gitlab_migrations == 'success'
  end
end
Sign up to request clarification or add additional context in comments.

2 Comments

Hmm, I'll try this out and see how it works.. But out of curiosity, doesn't the return_array.insert result in a TypeError since it will look for integer instead? like should it be return_array.push?
Btw, changing return_array.push(check_item) to return_array.push(check_item.check)` worked like a charm! Thanks again! <3

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.