Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
def users_list  
  html = ''  
  self.users.each do |user|  
    html << user.link_avatar_tag  

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)? – fantgeass 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 – fantgeass Jan 8 '13 at 12:54
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
share|improve this answer
I change code a bit & that works: – fantgeass 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


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.