When you call .html_safe on the first String, it returns a SafeBuffer if it is a safe string.
Now, when you add another string, and if it is a plain string and you have not called .html_safe on it, the buffer escapes it first, then concatenates it.
This is why, you are getting these results.
to my mind:
The concatenation of two Strings returns a new Object, which should
only be html_safe, if both parts are html_safe, otherwise html_UNsafe.
Yes, you are right. When you do String1 + String 2, it returns a new String object. You can check it in irb, by calling .object_id on the String1, String2 and String1+String2.
> When you call .html_safe on the first String, it returns a SafeBuffer if it
is a safe string.
Now, when you add another string, and if it is a plain string and you have
not called .html_safe on it, the buffer escapes it first, then concatenates
it.
i looked up the implementation and it is exactly what you pointed out.
If someone wants to know what happens(activesupport-3.0.4/lib/
active_support/core_ext/string/output_safety.rb):
def concat(value)
if value.html_safe?
super(value)
else
super(ERB::Util.h(value))
end
end
def +(other)
dup.concat(other)
end
This is why, you are getting these results.
Thanks for your quick reply! Now it makes sense to me!