Search code examples
ruby-on-railsrubysanitizationtruncate

Ruby-on-Rails: Mixing Sanitize and Truncate can be a dirty thing


So stand alone I get what I need. But I want to truncate it, my dynamic text comes out with dirty text globbered with Microsoft Word garbage.

An Example :

≪! [If Gte Mso 9]>≪Xml>  ≪Br /> ≪O:Office Document Settings>  ≪Br /> ≪O:Allow Png/>  ≪Br /> ≪/O:Off...

So how do I get the best of both worlds? Is there a shorthand ruby way to do this? For example a gsub statement that would clip off everything after the 125th char?


Solution

  • if you just want to slice, you can

    >> long_ugly_string = "omg this is a long string"
    => "omg this is a long string"
    >> long_ugly_string[10..-1]
    => "s a long string"
    

    Reference: http://ruby-doc.org/core/classes/String.html#M000771

    so, you are just specifying the starting character (10) and the ending character (-1 tells it to go to the end of the string).