Why doesn't work

why method pierw doesn’t run?

class Wielomian
attr_accessor :a, :b, :c

def initialize
@d
end

def +(other)
result = Wielomian.new
result.a = self.a + other.a
result.b = self.b + other.b
result.c = self.c + other.c
result
end

def -(other)
result = Wielomian.new
result.a = self.a - other.a
result.b = self.b - other.b
result.c = self.c - other.c
result
end

def *(other)
result = Wielomian.new
result.a = self.a * other.a
result.b = self.b * other.b
result.c = self.c * other.c
result
end

def /(other)
result = Wielomian.new
result.a = self.a / other.a
result.b = self.b / other.b
result.c = self.c / other.c
result
end

def delta
@d = b**2 - (4ac)
end

def pierw
x1 = -(b - (Math.sqrt(@d))/2*a)
end

end

On Thu, Feb 9, 2012 at 4:17 PM, luk malcik [email protected] wrote:

def +(other)
result.b = self.b - other.b
end
@d = b**2 - (4ac)
end

def pierw
x1 = -(b - (Math.sqrt(@d))/2*a)
end

end

There are several strange things here. First of all, in initialize, @d
evaluates to nil and doesn’t setup anything at all. You define
attr_accessor for a, b and c, so I guess the client code does:

w = Wielomian.new
w.a = 3
w.b = 4
w.c = 5

If you then call w.pierw, @d is still nil. You might be better off
(but we need more information) to assign a, b and c in the initialize
method, which right away could calculate the value for @d, setting
your object in a complete state:

class Wielomian
def initialize a,b,c
@a = a
@b = b
@c = c
delta
end

… the rest

end

w = Wieloman(3,4,5)
w.pierw

Also, in the pierw method, assigning to a local variable x1 that is
not used is useless. You can drop the assignment.

Hope this helps,

Jesus.

On Thu, Feb 9, 2012 at 4:17 PM, luk malcik [email protected] wrote:

def pierw
x1 = -(b - (Math.sqrt(@d))/2*a)

If this is supposed to be the solutions of a.x^2 + b.x + c = 0,
then it would be

x1 = -(b + Math.sqrt(discriminator))/(2a)
x2 = -(b - Math.sqrt(discriminator))/(2
a)

I think you have a bracket wrong …

HTH,

Peter


*** Available for a new project ***

Peter V.
http://twitter.com/peter_v
http://rails.vandenabeele.com
http://coderwall.com/peter_v

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs