Search code examples
rubyfunctionunexpected-token

Ruby : unexpected ',', expecting '.' or &. or :: or '['


I'm currently trying to implement a mathematic method to approximate f(x) = 0. I've already implemented it in some languages and I want to do it in ruby now just for training. But I have this error that I really does'nt understand Here is my code

def fonction (x)
    return (x ** 3) + 4 * (x ** 2) - 10
end

def derive (x)
    return 3 * (x ** 2) + 8 * x
end

def newton(f, fPrime, n, u0)
    if n == 0 then
        return u0
    else
        uN = newton (f, fPrime, (n - 1), u0)
        return uN - f(uN) / fPrime(uN)
    end
end

for i in 0..6
    puts (newton (fonction, derive, i, 2))
end

Solution

  • i think there is space on newton method call

    uN = newton (f, fPrime, (n - 1), u0) # there is space after newton
    

    also in this one

    for i in 0..6
        puts (newton (fonction, derive, i, 2)) # there is space after newton
    end
    

    try remove it, and you will see another error i guess, i try it on repl