#### Topic: I don't understand what's wrong.. any help please :)

class Complex
def initialization (real, imaginary)
@real = real
@imaginary = imaginary
@angle = Math.atan((float)imaginary/real)
end
def +c
return Complex.new(c.real + real, c.imaginary + imaginary)
end
def -c
return Complex.new(real - c.real, imaginary - c.imaginary)
end
def *c
return complex.new(real * c.real - imaginary * c.imaginary, real * c.imaginary + imaginary * c.real)
end
def conjugate
temp = @real - @imaginary # try without @ to use accessor later
return Self * temp
end
@angle = angle

def /c
temp = Complex.new(0,0)
return temp
end

end

#### Re: I don't understand what's wrong.. any help please :)

At least you should correct your cast operator - instead of using casting in c-style you should use .to_f method for conversion to float.

``imaginary.to_f/real``

Beside this, you missed final 'end' in 'polar_to_rectangular' method

Update: I've completely forgotten about existing Float method in the Kernel module which you can use something like this:

``Float(imaginary)``

As far as I know, for this case Float simple call 'to_f' method

Last edited by WarHog (2011-08-22 17:38:10)

#### Re: I don't understand what's wrong.. any help please :)

class Complex
def initialization (real, imaginary)
@real = real
@imaginary = imaginary
@angle = Math.atan((imaginary.to_f/real)) # c-style casting (float) sth ## doesn't work here in RUBY It's to_f/real method
end
def +c
return Complex.new(c.real + real, c.imaginary + imaginary)
end
def -c
return Complex.new(real - c.real, imaginary - c.imaginary)
end
def *c
return complex.new(real * c.real - imaginary * c.imaginary, real * c.imaginary + imaginary * c.real)
end
def conjugate
temp = @real - @imaginary # try without @ to use accessor later
return Self * temp
end
@angle = angle
end
def /c
temp = Complex.new(0,0)
return temp
end

end

Complex mine(1,1)
puts(mine.real)
# error undefined mine?? what have I done wrong?

#### Re: I don't understand what's wrong.. any help please :)

1) There is already existing class Complex. If you really need to create your own class for working with complex numbers, you have to rename it
2) You should rename 'initialization' method to 'initialize'
3) For creation new object you should use 'new' method

``mine = MyComplex.new(1, 1)``

#### Re: I don't understand what's wrong.. any help please :)

thank you very much war hog