Created
February 17, 2010 12:43
-
-
Save julian7/306566 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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