On Jun 24, 9:59 am, simon180 <[email protected]> wrote: > The example code in my view is as follows: > > <%= survey_response.median([6,4,5,4,4,2]) %> > Then in survey_response.rb model the methods are as follows: > > def mean(array) > array.inject(array.inject(0) { |sum, x| sum += x } / > array.size.to_f) > end
You've got two calls to inject there, but only the inner one has a block. Not sure what the second call to inject should be doing - i don't think you need it at all. Fred > > def median(array,already_sorted=false) > return nil if array.empty? > array = array.sort unless already_sorted > m_pos = array.size / 2 > return array.size % 2 == 1 ? array[m_pos] : mean(array > [m_pos-1..m_pos]) > end > --~--~---------~--~----~------------~-------~--~----~ 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

