# Const initialization

**URL:** <https://www.ruby-forum.com/t/const-initialization/82185>\
**Category:** Ruby\
**Created:** [January 17, 2007, 10:24pm UTC](https://www.ruby-forum.com/t/const-initialization/82185 "2007-01-17T22:24:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [January 17, 2007, 10:24pm UTC](https://www.ruby-forum.com/t/const-initialization/82185/1 "2007-01-17T22:24:54Z")

</div>

I think ruby does something strange when it decides if certain constants  
have been initialized or not. Take this simple case:

#test.rb  
require ‘const’  
require ‘/home/dave/foo/const’

#const.rb  
DAVE=1

When I run this I get:

/home/dave/foo/const.rb:1: warning: already initialized constant DAVE

…and yes, const.rb is the same one used for both test1.rb and  
test2.rb. It seems like ruby hashes the paths to the source files and  
complains when they are different - even if both paths reference the  
same file in the filesystem. Is there a good way to avoid this? Thanks!

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [January 17, 2007, 10:46pm UTC](https://www.ruby-forum.com/t/const-initialization/82185/2 "2007-01-17T22:46:45Z")

</div>

David W. wrote:

> I think ruby does something strange when it decides if certain constants  
> have been initialized or not.  
> [snip]

It has nothing to do with constants in particular, but (as you were  
referring to at the end) whether or not Ruby thinks two required files  
are the same. See this:

C:\>type tmp.rb  
p( require( ‘const’ ) )  
p( require( ‘const.rb’ ) )  
p( require( ‘c:/const.rb’ ) )

C:\>ruby tmp.rb  
true  
false  
c:/const.rb:1: warning: already initialized constant FOO  
true

The ‘false’ means that Ruby could tell that it was the same file, and  
didn’t re-include it. The last ‘true’ means that Ruby didn’t realize  
that the two path specifications ended pointed to the same file, and  
re-included it. That’s when the warning goes off.

---

<div class="post-metadata">

**Author:** ![dawillis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@dawillis](https://www.ruby-forum.com/u/dawillis)\
**Post date:** [January 17, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/const-initialization/82185/3 "2007-01-17T23:09:48Z")

</div>

On Thu, 18 Jan 2007, David W. wrote:

> I think ruby does something strange when it decides if certain constants  
> have been initialized or not. Take this simple case:
> 
> #test.rb  
> require ‘const’  
> require ‘/home/dave/foo/const’

require File.expand\_path(‘const’)  
require File.expand\_path(‘/home/dave/foo/const’)

> complains when they are different - even if both paths reference the  
> same file in the filesystem. Is there a good way to avoid this? Thanks!
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

-a
