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 - (4*a*c)
end
def pierw
x1 = -(b - (Math.sqrt(@d))/2*a)
end
end
···
--
Posted via http://www.ruby-forum.com/.
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 <aport99@gmail.com> wrote:
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 - (4*a*c)
end
def pierw
x1 = -(b - (Math.sqrt(@d))/2*a)
end
end
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))/(2*a)
x2 = -(b - Math.sqrt(discriminator))/(2*a)
I think you have a bracket wrong ...
HTH,
Peter
···
On Thu, Feb 9, 2012 at 4:17 PM, luk malcik <aport99@gmail.com> wrote:
def pierw
x1 = -(b - (Math.sqrt(@d))/2*a)
--
*** Available for a new project ***
Peter Vandenabeele
http://twitter.com/peter_v
http://rails.vandenabeele.com
http://coderwall.com/peter_v