Refactor ruby each-code

def users_list
html = ''
self.users.each do |user|
end
html.html_safe
end


I feel that it is possible to write shorter.

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

• 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 github.com/drapergem/draper – fantgeass Jan 8 '13 at 12:54

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

def users_list
end

• I change code a bit & that works: self.users.map(&:link_avatar_tag).join.html_safe – 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