Forum: Ruby on Rails trouble accessing ruby constants globally

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9d12e24c0a3aebe28b58682d517acdfb?d=identicon&s=25 ybernet@msn.com (Guest)
on 2008-11-26 03:14
(Received via mailing list)
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
C7935335b15e9c363b8decb26eb6fcfa?d=identicon&s=25 Mr. Bless (Guest)
on 2008-11-26 06:31
(Received via mailing list)
Why don't you write File::NUM_SECTIONS_TO_PARSE
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-11-26 14:02
(Received via mailing list)
On Nov 26, 2:13 am, "yber...@msn.com" <yber...@msn.com> 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
9d12e24c0a3aebe28b58682d517acdfb?d=identicon&s=25 ybernet@msn.com (Guest)
on 2008-11-26 15:36
(Received via mailing list)
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 Cheung <frederick.che...@gmail.com>
This topic is locked and can not be replied to.