2010-11-02 32 views
1
sum.push(100) 
sum.push(",") 
sum.push(200) 
allsum = sum.split(",") 

while i < 2 
    totalsum = totalsum + allsum[i] 
    i = i+ 1 
end 

爲了這個,我正在錯誤的 數組不能在軌道上被強迫在長整數紅寶石 任何人可以幫助我在此數組不能在軌道上被強迫在長整數紅寶石

+0

你可以在數組上調用split()嗎? – zengr 2010-11-02 09:39:53

回答

2
sum = [100, ",", 200] 
i = 0 
totalsum = 0 

for i in 0..sum.length-1 do 
    if sum[i].kind_of? Integer 
    totalsum = totalsum + sum[i] 
    end 
end 

puts totalsum 

我不確定Rails的方式。但是是使用Ruby的解決方案之一。

+0

http://ideone.com/toXI4 – zengr 2010-11-02 09:58:39

+0

當我做了總結[我] [0]其工作,你可以告訴我爲什麼它的2維。 – 2010-11-02 10:28:09

+0

請發佈您正在嘗試運行的完整代碼。 – zengr 2010-11-02 10:30:48