Inheritance one more time

Hi, similar topic was here but still I can’t do that. This is my code:

Square = Struct.new(:x,:y,:a) do

include Domieszka

def pole

p = a**2

end

def obwod

o = 4*a

end

def pole=(a)

self.a = a

end

def obwod=(a)

self.a = a

end

end

class Rectangle<Square

include Domieszka

def pole=(b)

self.b = b
end
def pole
p=a*b

end

Why there is no possibility to write :
r = Rectangle.new
r.pole = 3
“Undefinied method error…”

Domieszka is module declared at the beginning of my code, I dont put it
here

On Sun, Feb 5, 2012 at 4:48 PM, luk malcik [email protected] wrote:

end

p=a*b
Domieszka is module declared at the beginning of my code, I dont put it
here

I’m not sure what’s in your Domieszka module, but in the code you
pasted, there are no methods b or b= in Rectangle; and there are no
methods a or a= in Square. The missing Rectangle#b= is why r.pole = 3
doesn’t work.

Also, since you include Domieszka in the superclass, there’s no need
to include it again in the subclass.

That’s full of code. I think there is no problem with Domieszka.

module Domieszka

attr_accessor :x, :y

def moveto(x,y)

     @x = x

     @y = y

     end

end

Circle = Struct.new(:x,:y,:r) do

include Domieszka

def field

p = Math::PI * r**2

end

def field=®

self.r = r

end

def obwod

o = 2Math::PIr

end

def obwod=®

self.r = r

end

end

Square = Struct.new(:x,:y,:a) do

include Domieszka

def field

p = a**2

end

def obwod

o = 4*a

end

def field=(a)

self.a = a

end

def obwod=(a)

self.a = a

end

end

class Rectangle<Square

def fieldRect=(b)

self.b = b
end
def fieldRect
p=a*b

end
end

On Sun, Feb 5, 2012 at 7:21 PM, luk malcik [email protected] wrote:

Square = Struct.new(:x,:y,:a) do

Oh, right – I forgot Square is a struct with a and a= defined on it.
Since you have
self.b = b
you need to define b=, e.g. by defining an accessor:
attr_accessor :b

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