Hi, I''m attempting to loop through an array in a method in a model, Product.rb, and return a value if a certain conditional is met. I''m having some trouble however, as it doesn''t seem to be working as expected. The method from my model is as follows: def product_availability_label QCC.config(:product_availability).each do |av| "#{av.first}" if av.last == self.availability end end The product availability array is declared elsewhere as: config :product_availability, [[''Immediate'',1],[''1 Week'',2],[''2 Weeks'',3],[''3 Weeks'',4],[''1 Month'',5]] Returning a .type on QCC.config(:product_availability) from within Product.rb confirms it''s an array and is the array I''m expecting, however the output from the product_availability_label method is "Immediate11 Week22 Weeks33 Weeks41 Month5", rather than the expected "Immediate" (in the instance where self.availability is 1). No errors are raised. I know I could do this from within a helper, but before I make any alterations, I''d rather understand what''s going on. I''m using Ruby 1.8.5, Rails 1.1.6 on OSX should it make any difference. Thanks :) -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2006-Nov-29 11:45 UTC
Re: Unexpected behaviour looping an array in a model.
This is normal: The each method returns the array it was called on Just add a return statement. Fred -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> This is normal: The each method returns the array it was called on > Just add a return statement. > > FredOh dear. Lack of caffeine I think. Thanks Fred! -- 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 -~----------~----~----~----~------~----~------~--~---