# Undefined method (noMethodError)

**URL:** <https://www.ruby-forum.com/t/undefined-method-nomethoderror/63640>\
**Category:** Ruby\
**Created:** [June 16, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/undefined-method-nomethoderror/63640 "2006-06-16T21:37:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 16, 2006, 9:37pm UTC](https://www.ruby-forum.com/t/undefined-method-nomethoderror/63640/1 "2006-06-16T21:37:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 16, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/undefined-method-nomethoderror/63640/2 "2006-06-16T21:56:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 16, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/undefined-method-nomethoderror/63640/3 "2006-06-16T22:25:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 16, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/undefined-method-nomethoderror/63640/4 "2006-06-16T22:47:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 16, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/undefined-method-nomethoderror/63640/5 "2006-06-16T22:35:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 16, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/undefined-method-nomethoderror/63640/6 "2006-06-16T23:00:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 16, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/undefined-method-nomethoderror/63640/7 "2006-06-16T22:47:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 16, 2006, 11:10pm UTC](https://www.ruby-forum.com/t/undefined-method-nomethoderror/63640/8 "2006-06-16T23:10:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 16, 2006, 11:10pm UTC](https://www.ruby-forum.com/t/undefined-method-nomethoderror/63640/9 "2006-06-16T23:10:13Z")

</div>

> This is what I meant:

Thanks a lot Stefan.

---

<div class="post-metadata">

**Author:** ![parvg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@parvg](https://www.ruby-forum.com/u/parvg)\
**Post date:** [June 16, 2006, 11:07pm UTC](https://www.ruby-forum.com/t/undefined-method-nomethoderror/63640/10 "2006-06-16T23:07:46Z")

</div>

> 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
