\$\begingroup\$
\$\endgroup\$
6
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)?
tokland
11.2k1 gold badge21 silver badges26 bronze badges
1 Answer 1
\$\begingroup\$
\$\endgroup\$
2
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
answered Jan 7, 2013 at 16:36
-
1\$\begingroup\$ I change code a bit & that works: self.users.map(&:link_avatar_tag).join.html_safe \$\endgroup\$fantgeass– fantgeass2013年01月07日 16:47:14 +00:00Commented Jan 7, 2013 at 16:47
-
\$\begingroup\$ mmm, yes, you are right, here
safe_join
is not necessary, edited. \$\endgroup\$tokland– tokland2013年01月07日 17:05:46 +00:00Commented Jan 7, 2013 at 17:05
lang-rb
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. \$\endgroup\$html_safe
comes from. \$\endgroup\$self.users
, is this a model? a helper? \$\endgroup\$