Warning message about "initialized constant"

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’) 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

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.

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


1 ftp = Net::FTP.open(‘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

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.

On 23/06/06, Peter B. [email protected] 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

Farrel L. wrote:

On 23/06/06, Peter B. [email protected] 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!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs