Kle*_* S. 0 ruby refactoring ruby-on-rails
给出以下辅助方法.
def link_tags(collection)
tags = collection.split(',')
tags.map.each do |tag|
if tag == tags.last
content_tag(:a, tag, href: tags_filter_post_path(tag) )
else
content_tag(:a, tag, href: tags_filter_post_path(tag) ) + ', '
end
end.reduce(:<<)
end
Run Code Online (Sandbox Code Playgroud)
我怎么能对此做一点重构?
编辑:重构后的最终代码建议.
def link_tags(collection)
collection.split(',').collect do |tag|
link = ""
link += link_to tag, tags_filter_post_path(tag)
end.join(', ').html_safe
end
Run Code Online (Sandbox Code Playgroud)
def link_tags(collection)
collection.split(',').map do |tag|
link_to tag, tag_filter_post_path(tag)
end.join(', ')
end
Run Code Online (Sandbox Code Playgroud)