Shrikanth Hathwar
Shrikanth Hathwar

Reputation: 1160

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

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 into Fixnum in ruby on rails can anybody help me on this

Upvotes: 1

Views: 4030

Answers (1)

zengr
zengr

Reputation: 38899

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.

Upvotes: 2

Related Questions