I'm new to ruby coming from Java/C#.
I keep on getting an error "undefined method `nextcell' for
TestClass:Class (NoMethodError)" , when i try to run the code below.
if somebody can provide some insight, that would be appreciated.
class TestClass
def nextcell
col = 'A'
row = 1
maxCol = 'H'
maxRow = 5
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
nextcell()
nextcell()
end
The reason why i'm calling method nextcell() twice is to see if the
state of the variable has changed. is this the right approach or are my
variable declartions incorrect?
I keep on getting an error "undefined method `nextcell' for
TestClass:Class (NoMethodError)" , when i try to run the code below.
if somebody can provide some insight, that would be appreciated.
class TestClass
def nextcell
col = 'A'
row = 1
maxCol = 'H'
maxRow = 5
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
nextcell()
nextcell()
end
The reason why i'm calling method nextcell() twice is to see if the
state of the variable has changed. is this the right approach or are my
variable declartions incorrect?
You defined nextcell (I'd prefer next_cell) as an instance method. You
can only call it on an object of class TestClass.
If you comment the 2 calls to nextcell you can try the following:
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 @col = 'A' @row = 1 @maxCol = 'H' @maxRow = 5 @myNextCell = ''
end
def nextcell
@col = 'A' @row = 1 @maxCol = 'H' @maxRow = 5
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
end
Um, that's because you reset @col to 'A' every time you call nextcell.
get rid of that line in nextcell
···
On Jun 16, 2006, at 4:25 PM, 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:
[snip]
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
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
Um, that's because you reset @col to 'A' every time you call nextcell.
get rid of that line in nextcell
getting rid line *** @col = 'A' *** results in variable @col being nil.
And i can't compare nil with @maxCol (@maxCol = 'H').
This is because you mistyped initialize (I always do this). If you
define @col in initialize it will be happy to be available through all
instance methods.