# Warning message about "initialized constant"

**URL:** <https://www.ruby-forum.com/t/warning-message-about-initialized-constant/64258>\
**Category:** Ruby\
**Created:** [June 23, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/warning-message-about-initialized-constant/64258 "2006-06-23T16:54:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [June 23, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/warning-message-about-initialized-constant/64258/1 "2006-06-23T16:54:55Z")

</div>

This is part of a script I use that simply ftps a bunch of files over to  
a UNIX server at my company. It does work, but, I keep getting a warning  
message from RUBY about “rb:20:warning: already initialized constant  
BWDFile”. The line 20 it’s referring to is my line 5 below.

## Thanks for any help.

…  
1 ftp = Net::FTP.open(‘[quark.bna.com](http://quark.bna.com)’) do |ftp|  
2 ftp.login(‘xxxxxx’,‘xxxxxxxx’)  
3 ftp.chdir(‘/xxxxx/xxx/xxd/xxxx’)

4 BWDFiles=Dir.glob(“\*.{pdf,gif}”)  
5 BWDFiles.each do |BWDFile|  
6 ftp.putbinaryfile(BWDFile)  
7 FileUtils.rm(BWDFile)  
8 end  
9 end

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [June 23, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/warning-message-about-initialized-constant/64258/2 "2006-06-23T17:06:21Z")

</div>

Peter B. wrote:

> This is part of a script I use that simply ftps a bunch of files over to  
> a UNIX server at my company. It does work, but, I keep getting a warning  
> message from RUBY about “rb:20:warning: already initialized constant  
> BWDFile”. The line 20 it’s referring to is my line 5 below.

[snip]

In Ruby, identifiers starting with uppercase are constants. Constants,  
different from variables, are not supposed to change. Thus the  
warning, because you try to use a variable as a constant

Additionally, camelCase is usually reserved for class names (as a  
convention), it’s considered better style to use snake\_case, in your  
case you are better off using “bwd\_file” as the variable name.

t.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [June 23, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/warning-message-about-initialized-constant/64258/3 "2006-06-23T17:06:31Z")

</div>

On Jun 23, 2006, at 9:55 AM, Peter B. wrote:

> …  
> 1 ftp = Net::FTP.open(‘[quark.bna.com](http://quark.bna.com)’) do |ftp|  
> 2 ftp.login(‘xxxxxx’,‘xxxxxxxx’)  
> 3 ftp.chdir(‘/xxxxx/xxx/xxd/xxxx’)
> 
> 4 BWDFiles=Dir.glob(“\*.{pdf,gif}”)  
> 5 BWDFiles.each do |BWDFile|  
> 6 ftp.putbinaryfile(BWDFile)  
> 7 FileUtils.rm(BWDFile)  
> 8 end  
> 9 end

One of the conventions of the Ruby language is that capitalized  
variable names are considered constants. You can fix your problem by  
changing BWDFiles to bwd\_files and BWDFile (which changes each  
iteration therefore it isn’t constant) to bwd\_file. Ruby only  
CamelCases class names, never variables. If you stick with the  
conventions, other rubyists reading your code will have an easier  
time deciphering it.

> 4 bwd\_files=Dir.glob(“\*.{pdf,gif}”)  
> 5 bwd\_files.each do |bwd\_file|  
> 6 ftp.putbinaryfile(bwd\_file)  
> 7 FileUtils.rm(bwd\_file)  
> 8 end  
> 9 end

cr

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [June 23, 2006, 5:09pm UTC](https://www.ruby-forum.com/t/warning-message-about-initialized-constant/64258/4 "2006-06-23T17:09:42Z")

</div>

Anton ‘tony’ Bangratz wrote:

I hate replying to myself. But yikes:

[snip]

> In Ruby, identifiers starting with uppercase are constants. Constants,  
> different from variables, are not supposed to change. Thus the  
> warning, because you try to use a variable as a constant

this should read:

“because you try to use a constant as a variable.”

Sorry,

t.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [June 23, 2006, 5:09pm UTC](https://www.ruby-forum.com/t/warning-message-about-initialized-constant/64258/5 "2006-06-23T17:09:56Z")

</div>

On 23/06/06, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 3 ftp.chdir(‘/xxxxx/xxx/xxd/xxxx’)

Variable names beginning with uppercase are treated as constants by  
the Ruby interpreter. In the first iteration of the each loop the  
line:  
BWDFiles.each do |BWDFile|  
assigns to BWDFile the first element of BWDFiles. In the next  
iteration it tries to set BWDFiles to the second element but because  
BWDFile is a constant it generates the warning.

Farrel

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [June 23, 2006, 6:47pm UTC](https://www.ruby-forum.com/t/warning-message-about-initialized-constant/64258/6 "2006-06-23T18:47:38Z")

</div>

Farrel L. wrote:

> On 23/06/06, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 3 ftp.chdir(‘/xxxxx/xxx/xxd/xxxx’)
> 
> Variable names beginning with uppercase are treated as constants by  
> the Ruby interpreter. In the first iteration of the each loop the  
> line:  
> BWDFiles.each do |BWDFile|  
> assigns to BWDFile the first element of BWDFiles. In the next  
> iteration it tries to set BWDFiles to the second element but because  
> BWDFile is a constant it generates the warning.
> 
> Farrel

Thank you to all of you. I guess this is pretty important stuff. Because  
I’m a Windows guy, obviously, case has never meant much to me. It’s  
interesting that I’ve never run into this problem before. Granted, I’ve  
only been doing RUBY for 3 months or so, but, obviously, all of my  
previous variable names must’ve been lowercase!

Thanks again!
