ybernet-uAjRD0nVeow@public.gmane.org
2008-Nov-26 02:13 UTC
trouble accessing ruby constants globally
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Why don''t you write File::NUM_SECTIONS_TO_PARSE On Nov 26, 7:13 am, "yber...-uAjRD0nVeow@public.gmane.org" <yber...-uAjRD0nVeow@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 26, 2:13 am, "yber...-uAjRD0nVeow@public.gmane.org" <yber...-uAjRD0nVeow@public.gmane.org> wrote:> 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. >Has that file been loaded at the point you try to use that constant? Fred> Yoram--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ybernet-uAjRD0nVeow@public.gmane.org
2008-Nov-26 14:35 UTC
Re: trouble accessing ruby constants globally
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...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Nov 26, 2:13 am, "yber...-uAjRD0nVeow@public.gmane.org" <yber...-uAjRD0nVeow@public.gmane.org> wrote: > > > 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. > > Has that file been loaded at the point you try to use that constant? > > Fred > > > Yoram > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---