Klasy

Hej, prosze o pomoc. Mam prosta klase:

[code]class Klasa
tab = Hash.new
def dodaj(var1, var2)
tab[var1.to_s] = var2.to_s
end
def odczyt(var1)
tab[var1.to_s]
end
end

a = Klasa.new

a.dodaj(11,“jeden”)
puts a.odczyt(11)[/code]
I wywala mi blad “undefined local method or variable tab”. Moglby mi ktos wyjasnic o co chodzi? Z góry dzieki, pozdrawiam

zmien wszedzie tab na @tab, bo teraz to jest zmienna lokalna i nie widać jej w metodach

zmienilam… i kolejny blad: “undefined method ‘[]=’ for nil: NilClass”

potrzebny jest jeszcze konstruktor,

musisz dodać metodę initialize i w niej utworzyć zmienną @tab

[code=ruby]class Klasa
def initialize
@tab = Hash.new
end
def dodaj(var1, var2)
@tab[var1.to_s] = var2.to_s
end
def odczyt(var1)
@tab[var1.to_s]
end
end

a = Klasa.new

a.dodaj(11,“jeden”)
puts a.odczyt(11)[/code]

Dzieki:)