def encode
self[:code] = self[:code].gsub(/\&/, "&").gsub(/\</, "<").gsub(/\>/, ">")
end
Refactorings
No refactoring yet !
Christoffer
February 14, 2010, February 14, 2010 08:27, permalink
There is a library for what you want to do at:
http://htmlentities.rubyforge.org/
Your solution is simple but not complete, since it doesn't take care of any special chars.
If you are using Rails you can go with this in yout views.
<%=h @code %>
Jits
February 14, 2010, February 14, 2010 11:11, permalink
This should work directly in a Rail application.
In other scenarios you may need to require the CGI library.
x = "<my> Text & Tales" y = CGI.escapeHTML(x)
I'm looking for an easier/better way to to have the code property html encoded. Any way for this to be shorter and less ugly?