我想能夠存儲變量到一個數組,然後通過它們循環,然後將結果存儲在數組中,並返回該新的數組。商店類初始化變量到數組然後循環通過它.. [ruby]
的代碼應該解釋什麼,我試圖做的更好:
這是我的嘗試:
# 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
我想不出我在做什麼錯了,因爲在錯誤上面的代碼結果。
編輯:所以問題是......我如何成功地將這些類初始化放入數組,然後將它們正確循環並將結果放入另一個數組中?
EDIT2:現在,我發現了以下錯誤(S):
TypeError - no implicit conversion of CheckString into Integer
EDIT3:所以,我能夠通過改變url_check_array.insert
到url_check_array.push
添加那些數組...但是,我現在的問題是,除了將相同的東西放入return_array
(以下內容)後,我所擁有的循環並沒有多大作用:
[#<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">]
而不是實際的結果..所以,我真正想要它做的是實際執行CheckString.new("#{gitlab_migrations}", 'success')
,然後將結果放入一個數組。
你有問題要問?你有什麼具體問題嗎?你是否收到錯誤信息或結果不同於你預計忘記包括在你的問題中? –
我會更新OP。 – Fadi
「Array#insert」的第一個參數應該是一個整數索引,但是您將一個CheckString對象傳遞給TypeError。 –