# Constants Question

**URL:** <https://www.ruby-forum.com/t/constants-question/104503>\
**Category:** Ruby\
**Created:** [May 18, 2007, 7:23pm UTC](https://www.ruby-forum.com/t/constants-question/104503 "2007-05-18T19:23:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71c47a/32.png) [@pallan](https://www.ruby-forum.com/u/pallan)\
**Post date:** [May 18, 2007, 7:23pm UTC](https://www.ruby-forum.com/t/constants-question/104503/1 "2007-05-18T19:23:32Z")

</div>

Hi,

I have building a small custom library and I want to be able to define a  
constant in each subclass identifying “valid” options. Each subclass  
will have its own list.

here is how I tried to do it, please ignore the literal values, this is  
just an example:

class Base  
def method(option)  
raise MyException unless VALID.include?(option)  
do\_with\_option(option)  
end  
end

class SubClassA \< Base  
VALID = [‘color’,‘size’,‘weight’]  
end

class SubClass \< Base  
VALID = [‘name’,‘phone’,‘email’]  
end

sub = SubClass.new  
sub.option(‘phone’)

This results in a “uninitialized constant VALID” error. Why is that?  
It is like the VALID constant is not being recognized in the subclass.

I would appreciate any direction on how to best handle what I am looking  
to do.

Thanks

Peer

---

<div class="post-metadata">

**Author:** ![pallan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/71c47a/32.png) [@pallan](https://www.ruby-forum.com/u/pallan)\
**Post date:** [May 18, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/constants-question/104503/2 "2007-05-18T20:13:01Z")

</div>

Peer A. wrote:

> def method(option)  
> VALID = [‘name’,‘phone’,‘email’]  
> end
> 
> sub = SubClass.new  
> sub.option(‘phone’)
> 
> This results in a “uninitialized constant VALID” error. Why is that?  
> It is like the VALID constant is not being recognized in the subclass.

Constants are statically scoped, so it really is unrecgnize in the  
context of Base.

Use this instead of VALID:

self.class::VALID

this forces dynamic lookup, and it will be found in the appropriate  
subclass.
