Hi, I''m using CountryCodes to get countries list for select box. It provides self.countries_for_select(*args) method exactly for this purpose. I''m using it in only one place in my view. So I was thinking about caching this data somehow, so it won''t have to be recreated every time the page is loaded. The problem is that the plugin initializes its main @countries variable in init.rb file by calling method CountryCodes.load_countries_from_yaml. Is there an easier way to cache this data than doing something like this: lib/country_codes.rb module CountryCodes ... def self.cache_countries_for_select @cached_countries_for_select = countries_for_select(''name'', ''a2'').sort.freeze end def self.cached_countries_for_select @cached_countries_for_select end ... end init.rb require ''country_codes'' CountryCodes.load_countries_from_yaml CountryCodes.cache_countries_for_select I don''t think that caching this data will make huge performance gain, I''m rather trying to do it to understand Ruby and Rails better. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The Ruby way of caching the result of a cost operation in a variable is: def self.countries_for_select @cached_countries_for_select ||= countries_for_select(''name'', ''a2'').sort.freeze end The "||=" returns the value in @cached_countries_for_select "or", if it is nil (will be first time), fetch the value and stores in the variable. This is pure Ruby, nothing to do with Rails. If you want to learn some cool Rails techniques, including the use of page/fragmant cache to better solve cases like this, I recommend http://railscasts.com On Mar 1, 11:43 am, szimek <szi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > I''m using CountryCodes to get countries list for select box. It > provides self.countries_for_select(*args) method exactly for this > purpose. I''m using it in only one place in my view. So I was thinking > about caching this data somehow, so it won''t have to be recreated > every time the page is loaded. > > The problem is that the plugin initializes its main @countries > variable in init.rb file by calling method > CountryCodes.load_countries_from_yaml. > > Is there an easier way to cache this data than doing something like > this: > > lib/country_codes.rb > module CountryCodes > ... > def self.cache_countries_for_select > @cached_countries_for_select = countries_for_select(''name'', > ''a2'').sort.freeze > end > def self.cached_countries_for_select > @cached_countries_for_select > end > ... > end > > init.rb > require ''country_codes'' > CountryCodes.load_countries_from_yaml > CountryCodes.cache_countries_for_select > > I don''t think that caching this data will make huge performance gain, > I''m rather trying to do it to understand Ruby and Rails better.--~--~---------~--~----~------------~-------~--~----~ 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 2 Mar, 14:19, Okada <carlos.ok...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> The Ruby way of caching the result of a cost operation in a variable > is: > > def self.countries_for_select > @cached_countries_for_select ||= countries_for_select(''name'', > ''a2'').sort.freeze > end > > The "||=" returns the value in @cached_countries_for_select "or", if > it is nil (will be first time), fetch the value > and stores in the variable. > > This is pure Ruby, nothing to do with Rails. > > If you want to learn some cool Rails techniques, including the use of > page/fragmantcacheto better solve cases like this, I recommendhttp://railscasts.com > > On Mar 1, 11:43 am, szimek <szi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > > I''m using CountryCodes to get countries list for select box. It > > provides self.countries_for_select(*args) method exactly for this > > purpose. I''m using it in only one place in my view. So I was thinking > > about caching this data somehow, so it won''t have to be recreated > > every time the page is loaded. > > > The problem is that theplugininitializes its main @countries > > variable in init.rb file by calling method > > CountryCodes.load_countries_from_yaml. > > > Is there an easier way tocachethis data than doing something like > > this: > > > lib/country_codes.rb > > module CountryCodes > > ... > > def self.cache_countries_for_select > > @cached_countries_for_select = countries_for_select(''name'', > > ''a2'').sort.freeze > > end > > def self.cached_countries_for_select > > @cached_countries_for_select > > end > > ... > > end > > > init.rb > > require ''country_codes'' > > CountryCodes.load_countries_from_yaml > > CountryCodes.cache_countries_for_select > > > I don''t think that caching this data will make huge performance gain, > > I''m rather trying to do it to understand Ruby and Rails better.Thanks! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---