我有以下代碼:
count = 0
3.times do |survey|
count = count 1
@psychological_safety_score_ count.to_s = Team.get_psychological_safety_score(@team, count)
end
這給了我錯誤 undefined method to_s= for 1:Integer
我也嘗試@psychological_safety_score_ #{count.to_S}過沒有運氣。
最終目標是獲取實體變數 psychological_safety_score_1...3
uj5u.com熱心網友回復:
Ruby 語言本身不允許您通過將資料插入變數名來計算變數名。
盡管如此,仍然可以做你想做的事,使用instance_variable_set.
score = Team.get_psychological_safety_score(@team, count)
variable = :"@psychological_safety_score_#{count}"
instance_variable_set variable, score
此外,count是多余的。Integer#times迭代self次數,產生塊的當前索引。它可以用作計數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/338271.html
標籤:红宝石
