How to truncate text in Ruby on Rails

Posted on 14 August 2006

It's really simple to truncate text in Rails, just pass the function the text, the max number of characters you want to show and a truncate string you want to display instead of the last 3 characters.

truncate(category.description, 40, "...")

Comments left...

  • You’d probably be happier with something like this, which will cut off text at a word boundary. You can put it in your application helper to use it in all your views.

    def snippet(thought)

    wordcount = 10

    thought.split[0..(wordcount-1)].join(" ") + (thought.split.size > wordcount ? “…” : "")

    end

    Chris Kampmeier at 22 Jan 07 at 20:40

  • @Chris thanks, I’m gunna use this. However, I modified it so that the wordcount is passable as a parameter:

    def snippet(thought, wordcount)
    thought.split[0..(wordcount-1)].join(" ") +(thought.split.size > wordcount ? “…” : "")
    end

    Also, just an fyi, when I pasted your code into Textmate it preserved the fancy quotes, which gave me about 30 seconds of WTF?

    Byron Bowerman at 30 Nov 07 at 16:53

  • Try this one out, the number in brackets is the number of chars to match:

    thought.gsub(/^(.{10}[\w.])(.)/) {$2.empty? ? $1 : $1 + ‘…’}

    It doesn’t break words and adds ‘…’ if necessary. Check out www.golark.com and click submit to really be impressed with the power of regex.

    lou at 06 Dec 07 at 21:42

  • Great, it took the asterisks out. Replace the a’s in the following string with asterisks:

    thought.gsub(/^(.{10}[\w.]a)(.a)/) {$2.empty? ? $1 : $1 + ‘…’}

    lou at 06 Dec 07 at 21:46

  • Seems all of them to be more complicated than …

    thought = (thought.length > wordcount) ? “#{thought[0..(wordcount – 1)] …” : thought

    Sam_dal at 02 Dec 08 at 15:24

  • Odd that the Ruby String class doesn’t have a method to truncate itself.

    Ed at 07 Dec 08 at 21:03

  • “1234567890”[0..4] => “12345”

    Simple is good at 09 Jan 09 at 07:45

  • “1234567890”.first(5)

    and there’s also
    “1234567890”.last(5)

    The Irish Penguin at 14 Jan 09 at 04:50

  • @lou:

    Put an m after the second slash to truncate multiline text appropriately

    thought.gsub(/^(.{10}[\w.]a)(.a)/m) {$2.empty? ? $1 : $1 + ‘…’}

    nice at 07 Jul 09 at 15:09

  • This will be deprecated, you need to use

    truncate(text, :length => 123, :omission =>"…")

    Michael Hendrickx at 14 Aug 09 at 01:42

  • asgw3.txt;4;15

    asgw3.txt;4;15 at 16 Sep 09 at 22:00

  • asgw3.txt;4;15

    asgw3.txt;4;15 at 16 Sep 09 at 22:01

  • Thanks for the info and comments. Very useful

    cheers
    http://www.movieteca.com

    Fabian Pena at 14 Oct 09 at 21:19

  • <%=course.description.length > 60 ? course.description[0..60] << ‘…’: course.description unless course.description.nil? %>

    This can be directly written in views ,which checks the length of description.

    je at 10 Feb 10 at 00:45

  • @je I think you are missing the point of the truncate method as it will do all that for you.

    Paul Sturgess at 10 Feb 10 at 03:03

  • It would be great if this worked on word boundaries, but still used a maximum number of chars for input. A lot of the comments don’t seem to get that the point of this is to put on the “…” conditionally.

    French Vocabulary at 22 Feb 10 at 23:50

Got something to say?