In the spirit of keeping my app DRY, i would like to store an array of strings, and call this array in a few views and possibly in my controller. Where is the best place to declare an application wide static variable? And is this possible? array = [''why should'', ''I have to copy this'', ''every time i want to use it''] -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
huard.elise-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Jan-04 20:57 UTC
Re: storing a static array
Well, if it''s not going to change, you make it a constant. Constants in ruby are in uppercase. MY_ARRAY = [''why should'', ''I have to copy this'', ''every time i want to use > it''] Normally this should be enough, but if you want to go extra safe you can freeze it, since an array is an object (http://www.ruby-doc.org/ core/classes/Object.html#M000354). where to put it in Rails: one option is to declare it in a file in the lib directory. Rails loads what is in lib. To make sure it will never conflict with any other, you could put it into a module, and call it as ModuleName::MY_ARRAY Elise Richard Schneeman wrote:> In the spirit of keeping my app DRY, i would like to store an array of > strings, and call this array in a few views and possibly in my > controller. > > Where is the best place to declare an application wide static variable? > And is this possible? > > array = [''why should'', ''I have to copy this'', ''every time i want to use > it''] > -- > Posted via http://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You can also look at using an external configuration file instead of constants: http://www.stephenbartholomew.co.uk/2008/8/22/simple-application-wide-configuration-in-rails. Check out the comments for further ideas. Steve --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
huard.elise-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Jan-04 21:09 UTC
Re: storing a static array
you''re right, that is cleaner. On Jan 4, 10:01 pm, Steve Bartholomew <st...-LWB5c3/XHbdBDgjK7y7TUQ@public.gmane.org> wrote:> You can also look at using an external configuration file instead of > constants:http://www.stephenbartholomew.co.uk/2008/8/22/simple-application-wide.... > Check out the comments for further ideas. > > Steve--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
huard.elise-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Jan-04 21:12 UTC
Re: storing a static array
unless you really want an array, for some reason On Jan 4, 10:01 pm, Steve Bartholomew <st...-LWB5c3/XHbdBDgjK7y7TUQ@public.gmane.org> wrote:> You can also look at using an external configuration file instead of > constants:http://www.stephenbartholomew.co.uk/2008/8/22/simple-application-wide.... > Check out the comments for further ideas. > > Steve--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks a bunch for the help, for now its just one array, but sooner or later i''m sure I''ll want to branch out to your yml solution. Appreciate the help!!! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---