# CONST initialization

**URL:** <https://www.ruby-forum.com/t/const-initialization/108663>\
**Category:** Ruby\
**Created:** [July 5, 2007, 9:12pm UTC](https://www.ruby-forum.com/t/const-initialization/108663 "2007-07-05T21:12:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [July 5, 2007, 9:12pm UTC](https://www.ruby-forum.com/t/const-initialization/108663/1 "2007-07-05T21:12:24Z")

</div>

How come this works when I access Orders::PAYMENT\_TYPES

> > > > > > > > > > CODE  
> > > > > > > > > > class Order \< ActiveRecord::Base  
> > > > > > > > > > PAYMENT\_TYPES = [  
> > > > > > > > > > [“Check”, “check”],  
> > > > > > > > > > [“Credit card”, “cc”],  
> > > > > > > > > > [“Purchase order”, “po”]  
> > > > > > > > > > ]

has\_many :line\_items

validates\_presence\_of :name, :address, :email, :pay\_type  
validates\_inclusion\_of :pay\_type, :in =\> PAYMENT\_TYPES.map {|disp,  
value| value}

end  
\<\<\<\<\<\<\<\<\<\< CODE

But this doesn’t:

> > > > > > > > > > CODE  
> > > > > > > > > > class Order \< ActiveRecord::Base  
> > > > > > > > > > has\_many :line\_items

validates\_presence\_of :name, :address, :email, :pay\_type  
validates\_inclusion\_of :pay\_type, :in =\> PAYMENT\_TYPES.map {|disp,  
value| value}

PAYMENT\_TYPES = [  
[“Check”, “check”],  
[“Credit card”, “cc”],  
[“Purchase order”, “po”]  
]

end  
\<\<\<\<\<\<\<\<\<\< CODE

Error thrown:  
/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active\_support/dependencies.rb:477:in  
`const\_missing’

I have moved the PAYMENT\_TYPES map around and it even is okay if it’s  
after the has\_many :item\_lines accessor, but not if it’s after the  
‘validation’ accessors.

Thanks.

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [July 5, 2007, 9:52pm UTC](https://www.ruby-forum.com/t/const-initialization/108663/2 "2007-07-05T21:52:14Z")

</div>

On Jul 5, 2007, at 3:12 PM, Jean N. wrote:

> Thanks.

I think you’ve answered your own question, but you might not  
understand why. The ruby interpreter is “executing” each line within  
the class definition as it is encountered. If there’s no  
PAYMENT\_TYPES when the validates\_inclusion\_of line is reached, it  
really doesn’t matter that it _does_ appear later in the class  
definition.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![jeannibee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85e7bf/32.png) [@jeannibee](https://www.ruby-forum.com/u/jeannibee)\
**Post date:** [July 5, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/const-initialization/108663/3 "2007-07-05T23:12:57Z")

</div>

Rob B. wrote:

> On Jul 5, 2007, at 3:12 PM, Jean N. wrote:
> 
> > Thanks.
> 
> I think you’ve answered your own question, but you might not  
> understand why. The ruby interpreter is “executing” each line within  
> the class definition as it is encountered. If there’s no  
> PAYMENT\_TYPES when the validates\_inclusion\_of line is reached, it  
> really doesn’t matter that it _does_ appear later in the class  
> definition.
> 
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

Doh…

Sorry I"m still in ‘vm’ world w/ my Java code thinking that a Ruby class  
is ONLY a definition, I forget to realize it’s being interpreted as it’s  
being read.

Thanks for the wake up.
