# Constants Question

**URL:** <https://rubytalk.org/t/constants-question/37694>\
**Category:** ruby-talk\
**Created:** [18 May 2007 17:23 UTC](https://rubytalk.org/t/constants-question/37694 "2007-05-18T17:23:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peer\_Allan1](https://avatars.discourse-cdn.com/v4/letter/p/b5ac83/32.png) [@Peer\_Allan1](https://rubytalk.org/u/Peer_Allan1)\
**Post date:** [18 May 2007 17:23 UTC](https://rubytalk.org/t/constants-question/37694/1 "2007-05-18T17: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  
&nbsp;&nbsp;def method(option)  
&nbsp;&nbsp;&nbsp;&nbsp;raise MyException unless VALID.include?(option)  
&nbsp;&nbsp;&nbsp;&nbsp;do\_with\_option(option)  
&nbsp;&nbsp;end  
end

class SubClassA \< Base  
&nbsp;&nbsp;VALID = ['color','size','weight']  
end

class SubClass \< Base  
&nbsp;&nbsp;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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [18 May 2007 18:12 UTC](https://rubytalk.org/t/constants-question/37694/2 "2007-05-18T18:12:37Z")

</div>

Peer Allan wrote:

> 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  
> &nbsp;&nbsp;def method(option)  
> &nbsp;&nbsp;&nbsp;&nbsp;raise MyException unless VALID.include?(option)  
> &nbsp;&nbsp;&nbsp;&nbsp;do\_with\_option(option)  
> &nbsp;&nbsp;end  
> end
> 
> class SubClassA \< Base  
> &nbsp;&nbsp;VALID = ['color','size','weight']  
> end
> 
> class SubClass \< Base  
> &nbsp;&nbsp;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.

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
