Skip to content

Instantly share code, notes, and snippets.

@julian7
Created February 17, 2010 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julian7/306566 to your computer and use it in GitHub Desktop.
Save julian7/306566 to your computer and use it in GitHub Desktop.
class Array
def html_safe_join(separator)
acc = self.shift
acc = acc.try(:html_safe) || acc
self.inject(acc) do |a, elem|
a << separator
a << elem
end
acc
end
alias_method :join_to_string, :join
alias_method :join, :html_safe_join
end
# development environment (Rails 3.0.0.beta1)
a = ["a".html_safe, "b".html_safe]
b = a + ["c"]
puts a.join(" ".html_safe).html_safe?
# prints false
puts a.each_with_index.inject("".html_safe) { |a, elem| if elem[1] > 0; a << " ".html_safe; end; a << elem[0] }.html_safe?
# prints true
puts b.each_with_index.inject("".html_safe) { |a, elem| if elem[1] > 0; a << " ".html_safe; end; a << elem[0] }.html_safe?
# prints true!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment