From: Stefan Mahlitz Date: 2006-06-17T05:46:06+09:00 Subject: Re: undefined method (noMethodError) Parv G wrote: > Stefan Mahlitz wrote: > >> If you want the object to change its content you will have to define >> instance variables. >> >> def initialize >> @col = 'A' >> end >> >> In nextcell you have to change all occurences of 'col' to '@col'. >> >> Stefan > > Thanks Stefan. > > After using the instance variables through out i'm still getting the > same results for both of method class; the variable value is not > changing. > Here's the code: > class TestClass > def initilize this should be initialize > @col = 'A' > @row = 1 > @maxCol = 'H' > @maxRow = 5 > @myNextCell = '' > end > def nextcell > > @col = 'A' > @row = 1 > @maxCol = 'H' > @maxRow = 5 You need to get rid of the 4 lines above. > > if (@col < @maxCol) > @myNextCell = @col + @row.to_s > @col = @col.next > puts("Next Cell is #{@myNextCell}") > return @myNextCell > else > @row.next > @myNextCell = @col + @row.to_s > @col.next > return @myNextCell > end > end > > tc = TestClass.new > puts tc.nextcell.inspect > puts tc.nextcell.inspect move the 3 lines below the end > end This is what I meant: class TestClass # this method is executed if you call TestClass.new def initialize @col = 'A' @row = 1 @maxCol = 'H' @maxRow = 5 @myNextCell = '' end def next_cell if (@col < @maxCol) @myNextCell = @col + @row.to_s @col = @col.next puts("Next cell is #{@myNextCell}") else @row = @row.next @myNextCell = @col + @row.to_s @col = @col.next # this looks wrong # (@col increases on every call of # next_cell, maybe it should be reset to default ('A')?) end return @myNextCell end end tc = TestClass.new puts tc.inspect p tc cellB1 = tc.next_cell puts cellB1.inspect puts tc.inspect