Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
def users_list  
  html = ''  
  self.users.each do |user|  
    html << user.link_avatar_tag  
  end  
  html.html_safe  
end  

I feel that it is possible to write shorter.

Is it possible get rid of the temporary variable(html)?

share|improve this question
    
What class is user? html_safe is no standard method for String - What gems are you using? Or in short: Can you provide a MWE? And to answer: It looks ok for me. –  knut Jan 7 '13 at 15:08
    
Main topic of my question: Is it possible get rid of the temporary variable(html)? –  Fant Geass Jan 7 '13 at 15:30
    
I'll tag this rails, that's where html_safe comes from. –  tokland Jan 7 '13 at 16:41
    
one question about self.users, is this a model? a helper? –  tokland Jan 8 '13 at 8:39
    
This is TaskDecorator. Task has_and_belongs_to_many :users github.com/drapergem/draper –  Fant Geass Jan 8 '13 at 12:54

1 Answer 1

up vote 5 down vote accepted

You should use map (don't use each to accumulate. Check this page about functional programming):

def users_list  
  users.map(&:link_avatar_get).join.html_safe
end
share|improve this answer
1  
I change code a bit & that works: self.users.map(&:link_avatar_tag).join.html_safe –  Fant Geass Jan 7 '13 at 16:47
    
mmm, yes, you are right, here safe_join is not necessary, edited. –  tokland Jan 7 '13 at 17:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.