Re: Re: Outputting a string from an array of objects

Michael Pavling
Thu, 24 Feb 2011 18:24:01 -0000

On 24 February 2011 18:12, Peter D Bethke
<peter.bethke-ee4meeAH724@public.gmane.org> wrote:
> Thanks Fred!
>
> Does this look right? I''m still getting an error for some reason:

Always post the error... just saying "I''ve got one"
doesn''t help
people point out where you might be having problems (although in this
case, it''s ruby syntax... check the API for the ".collect"
method :-)

  def get_company_branches
    my_branches =  self.company.branches # you don''t really need these
intermediate variables... just use self.branches.collect....
    my_list = my_branches.collect { |x| x.branch_name }
    return my_list.join(", ") # "collect" returns an array,
so we can
join the array into a string, or maybe use the "to_sentence" extension
to prettify it
  end


...my previous method just does the same, a little more compact.