开发者

Array can't be coerced into Fixnum in ruby on rails

开发者 https://www.devze.com 2023-01-22 20:53 出处:网络
sum.push(100) sum.push(\",\") sum.push(200) allsum = sum.split(\",\") while i < 2 totalsum = totalsum + allsum[i]
sum.push(100)
sum.push(",")
sum.push(200)
allsum = sum.split(",")

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

for this i am getting the error as Array can't be coerced i开发者_如何学JAVAnto Fixnum in ruby on rails can anybody help me on this


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

I am not sure about the Rails way. But is one of the solution using Ruby.

0

精彩评论

暂无评论...
验证码 换一张
取 消