# Newbie Mixin Problem

**URL:** <https://www.ruby-forum.com/t/newbie-mixin-problem/48856>\
**Category:** Ruby\
**Created:** [November 25, 2005, 6:48pm UTC](https://www.ruby-forum.com/t/newbie-mixin-problem/48856 "2005-11-25T18:48:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathan.ruckwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonathan.ruckwood](https://www.ruby-forum.com/u/jonathan.ruckwood)\
**Post date:** [November 25, 2005, 6:48pm UTC](https://www.ruby-forum.com/t/newbie-mixin-problem/48856/1 "2005-11-25T18:48:23Z")

</div>

Hi, I’m new to Ruby and have done some reading on it. To learn more  
about it I’m attempting to re-write some of my Java code. However I  
have run into a problem with Modules/Mixins that I can’t seem to solve.  
My problem is like this; I have a module with the instance method  
validIndex? (and some constants) which is included into my other  
classes; Board, Strategy and Player.

module Common

MAX\_ENTRIES = 19682

def validIndex?(index)  
if index \>= 0 && index \<= MAX\_ENTRIES then  
return true  
else  
return false  
end  
end  
end

Each Player has two Strategies and Boards represent these. When  
creating a Board instance I can use validIndex?, however when creating  
a Strategy or Player (which creates new Boards) I get:

NoMethodError: undefined method `validIndex?' for Board:Class from /Users/jon_r/Projects/RbNoughtsCrosses/Board.rb:31:in`numToBoard’  
from /Users/jon\_r/Projects/RbNoughtsCrosses/Strategy.rb:13:in  
`initialize' from /Users/jon_r/Projects/RbNoughtsCrosses/Strategy.rb:12:in`each’  
from /Users/jon\_r/Projects/RbNoughtsCrosses/Strategy.rb:12:in  
`initialize' from /Users/jon_r/Projects/RbNoughtsCrosses/Player.rb:15:in`new’  
from /Users/jon\_r/Projects/RbNoughtsCrosses/Player.rb:15:in  
`initialize' from (irb):5:in`new’  
from (irb):5

But when I check each classes instance methods I see validIndex?  
listed. I’m confused, as in my eyes, it’s similar to the who\_am\_i?  
example given in the book. However there is still a lot for me to  
learn. Does anybody recognise this situation, or where I’m going wrong?  
I’m sorry if it’s stupid but I have tried to find solutions from other  
sources. Cheers.

Jonathan

---

<div class="post-metadata">

**Author:** ![jonathan.ruckwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonathan.ruckwood](https://www.ruby-forum.com/u/jonathan.ruckwood)\
**Post date:** [November 25, 2005, 6:56pm UTC](https://www.ruby-forum.com/t/newbie-mixin-problem/48856/2 "2005-11-25T18:56:30Z")

</div>

On Sat, 26 Nov 2005, jlr8 wrote:

> ```
> if index >= 0 && index <= MAX_ENTRIES then
> 
> ```
> 
> NoMethodError: undefined method `validIndex?’ for Board:Class  
> This suggests that you are trying to call validate on the  
> class itself: Board.validIndex() rather than some instance  
> aboard.validIndex()
> 
> But when I check each classes instance methods I see validIndex?

It would help to see the code where it’s called, if that isn’t the  
answer.  
Hugh

---

<div class="post-metadata">

**Author:** ![jonathan.ruckwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jonathan.ruckwood](https://www.ruby-forum.com/u/jonathan.ruckwood)\
**Post date:** [November 25, 2005, 7:00pm UTC](https://www.ruby-forum.com/t/newbie-mixin-problem/48856/3 "2005-11-25T19:00:35Z")

</div>

On Friday 25 November 2005 12:47 pm, jlr8 wrote:

> Each Player has two Strategies and Boards represent these. When  
> creating a Board instance I can use validIndex?, however when creating  
> a Strategy or Player (which creates new Boards) I get:
> 
> NoMethodError: undefined method `validIndex?’ for Board:Class

It looks to me like you’re calling validIndex? on Board, and not on an  
instance of a Board. Consider this code:

irb(main):001:0\> Object.foo  
NoMethodError: undefined method `foo’ for Object:Class  
from (irb):1

irb(main):002:0\> a = Object.new  
=\> #Object:0xb7ca26c8  
irb(main):003:0\> a.foo  
NoMethodError: undefined method `foo’ for #Object:0xb7ca26c8

See the difference in the errors?

Also, you can simplify your code a little bit:

def validIndex?(index)  
return index.between?(0, MAX\_ENTRIES)  
end
