# Trouble accessing ruby constants globally

**URL:** <https://www.ruby-forum.com/t/trouble-accessing-ruby-constants-globally/154169>\
**Category:** Rails\
**Created:** [November 26, 2008, 3:14am UTC](https://www.ruby-forum.com/t/trouble-accessing-ruby-constants-globally/154169 "2008-11-26T03:14:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 26, 2008, 3:14am UTC](https://www.ruby-forum.com/t/trouble-accessing-ruby-constants-globally/154169/1 "2008-11-26T03:14:22Z")

</div>

I’m trying to declare a constant in one file and access it in another.  
It doesn’t seem to be working.

Here’s my declaration (at the top of a file, remainder of file omitted  
for brevity):

NUM\_SECTIONS\_TO\_PARSE = 7

class UserDataFile \< File

def self.parse\_file(src\_name, parse\_progress)

```
    (other stuff follows)

```

Here’s where i’m trying to access it:

parse\_progress = Array.new(::NUM\_SECTIONS\_TO\_PARSE, ::NOT\_YET\_PARSED)

it fails with the message: uninitialized constant  
NUM\_SECTIONS\_TO\_PARSE

I’ve run into this problem several times before and always somehow  
hacked my way around it, but I want to get it right this time. Any  
help appreciated.

Yoram

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 26, 2008, 6:31am UTC](https://www.ruby-forum.com/t/trouble-accessing-ruby-constants-globally/154169/2 "2008-11-26T06:31:25Z")

</div>

Why don’t you write File::NUM\_SECTIONS\_TO\_PARSE

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 26, 2008, 2:02pm UTC](https://www.ruby-forum.com/t/trouble-accessing-ruby-constants-globally/154169/3 "2008-11-26T14:02:02Z")

</div>

On Nov 26, 2:13 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> def self.parse_file(src_name, parse_progress)
> 
> ```
> 
> I’ve run into this problem several times before and always somehow  
> hacked my way around it, but I want to get it right this time. Any  
> help appreciated.

Has that file been loaded at the point you try to use that constant?

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 26, 2008, 3:36pm UTC](https://www.ruby-forum.com/t/trouble-accessing-ruby-constants-globally/154169/4 "2008-11-26T15:36:11Z")

</div>

Thanks Fred. I guess I did not fully understand the sequence in which  
ruby loads files. I put a “require  
‘name\_of\_file\_with\_constant\_declaration’” at the top of the file in  
which I am using the constant and that fixed it.

Yoram

On Nov 26, 5:01 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
