Undefined method (noMethodError)

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?

Thanks

Parv G wrote:

I’m new to ruby coming from Java/C#.

Welcome.

    maxCol = 'H'
        col.next

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:

tc = TestClass.new
puts tc.nextcell.inspect
puts tc.nextcell.inspect

As you will notice both outputs are the same.

All your variables are local to the method nextcell, no state is
changed.

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

Stefan M. 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
@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

Thanks, appreciate your help.

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’).

On Jun 16, 2006, at 4:25 PM, Parv G wrote:

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]

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 21:47, Parv G wrote:

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’).

That’s because you spelled ‘initialize’ incorrectly.

matthew smillie.

Parv G wrote:

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

    @maxCol = 'H'
    @maxRow = 5

You need to get rid of the 4 lines above.

        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

Parv G wrote:

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.

Stefan

This is what I meant:

Thanks a lot Stefan.

That’s because you spelled ‘initialize’ incorrectly.

matthew smillie.

Bummer. That’ll do it… “The longer it takes to a solve a problem the
sillier the solution”
Thanks