# What is that method called?

**URL:** <https://rubytalk.org/t/what-is-that-method-called/50127>\
**Category:** ruby-talk\
**Created:** [11 November 2008 09:16 UTC](https://rubytalk.org/t/what-is-that-method-called/50127 "2008-11-11T09:16:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Trasbo](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@David\_Trasbo](https://rubytalk.org/u/David_Trasbo)\
**Post date:** [11 November 2008 09:16 UTC](https://rubytalk.org/t/what-is-that-method-called/50127/1 "2008-11-11T09:16:54Z")

</div>

I need the method that has the ability to turn a long string like this:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque  
pulvinar turpis a nisi. Cras id elit. Aliquam vitae pede nec lacus  
elementum lacinia. Ut aliquam vehicula sem.

into a shorter string like this:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque  
pulvinar turpis...

My problem is that I can't remember what the method is called. I looked  
through the whole Ruby String documentation  
([http://ruby-doc.org/core/classes/String.html](http://ruby-doc.org/core/classes/String.html)) but I couldn't find it.  
Can anyone remember what it's called?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Clifford\_Heath3](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@Clifford\_Heath3](https://rubytalk.org/u/Clifford_Heath3)\
**Post date:** [11 November 2008 09:57 UTC](https://rubytalk.org/t/what-is-that-method-called/50127/2 "2008-11-11T09:57:36Z")

</div>

David Trasbo wrote:

> I need the method that has the ability to turn a long string... into a shorter string...  
> My problem is that I can't remember what the method is called. I looked  
> through the whole Ruby String documentation...

There's no such method in Ruby. You might be thinking of the  
truncate method that's part of ActionView's TextHelper module.

Clifford Heath.

---

<div class="post-metadata">

**Author:** ![Peter\_Szinek3](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Peter\_Szinek3](https://rubytalk.org/u/Peter_Szinek3)\
**Post date:** [11 November 2008 10:04 UTC](https://rubytalk.org/t/what-is-that-method-called/50127/3 "2008-11-11T10:04:07Z")

</div>

I seriously doubt this is part of standard Ruby.

You have probably seen this in ActiveSupport and/or the 'english' gem - English::String#brief:

[http://english.rubyforge.org/rdoc/classes/English/String.html#M000096](http://english.rubyforge.org/rdoc/classes/English/String.html#M000096)

Cheers,  
Peter

> **···**
>
> On 2008.11.11., at 10:16, David Trasbo wrote:
> 
> > I need the method that has the ability to turn a long string like this:
> > 
> > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque  
> > pulvinar turpis a nisi. Cras id elit. Aliquam vitae pede nec lacus  
> > elementum lacinia. Ut aliquam vehicula sem.
> > 
> > into a shorter string like this:
> > 
> > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque  
> > pulvinar turpis...
> > 
> > My problem is that I can't remember what the method is called. I looked  
> > through the whole Ruby String documentation  
> > ([http://ruby-doc.org/core/classes/String.html\](http://ruby-doc.org/core/classes/String.html%5C)) but I couldn't find it.  
> > Can anyone remember what it's called?  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Mike\_Paul](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@Mike\_Paul](https://rubytalk.org/u/Mike_Paul)\
**Post date:** [11 November 2008 10:09 UTC](https://rubytalk.org/t/what-is-that-method-called/50127/4 "2008-11-11T10:09:50Z")

</div>

Hi David - just done this in Agile Development

truncate(string, n) where string is the string to be shortened and n is the  
number of characters you want it shortened to.

Cheers  
Mike

> **···**
>
> -----Original Message-----  
> From: [davidtrasbo@gmail.com](mailto:davidtrasbo@gmail.com) [[mailto:davidtrasbo@gmail.com](mailto:davidtrasbo@gmail.com)]  
> Sent: 11 November 2008 09:17  
> To: ruby-talk ML  
> Subject: What is that method called?
> 
> I need the method that has the ability to turn a long string like this:
> 
> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque  
> pulvinar turpis a nisi. Cras id elit. Aliquam vitae pede nec lacus  
> elementum lacinia. Ut aliquam vehicula sem.
> 
> into a shorter string like this:
> 
> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque  
> pulvinar turpis...
> 
> My problem is that I can't remember what the method is called. I looked  
> through the whole Ruby String documentation  
> ([http://ruby-doc.org/core/classes/String.html](http://ruby-doc.org/core/classes/String.html)) but I couldn't find it.  
> Can anyone remember what it's called?  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![David\_Trasbo](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@David\_Trasbo](https://rubytalk.org/u/David_Trasbo)\
**Post date:** [11 November 2008 10:47 UTC](https://rubytalk.org/t/what-is-that-method-called/50127/5 "2008-11-11T10:47:02Z")

</div>

Peter Szinek wrote:

> I seriously doubt this is part of standard Ruby.

You're right, it's not a part of the Ruby standard library:

Clifford Heath wrote:

> There's no such method in Ruby. You might be thinking of the  
> truncate method that's part of ActionView's TextHelper module.

truncate is what it's called. I was actually searching for 'rails  
StringHelper'... 🙂

Thanks for the replies!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Justin\_Collins](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Justin\_Collins](https://rubytalk.org/u/Justin_Collins)\
**Post date:** [11 November 2008 11:12 UTC](https://rubytalk.org/t/what-is-that-method-called/50127/6 "2008-11-11T11:12:22Z")

</div>

David Trasbo wrote:

> Peter Szinek wrote:  
> &nbsp;&nbsp;
> 
> > I seriously doubt this is part of standard Ruby.  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > You're right, it's not a part of the Ruby standard library:
> 
> Clifford Heath wrote:  
> &nbsp;&nbsp;
> 
> > There's no such method in Ruby. You might be thinking of the  
> > truncate method that's part of ActionView's TextHelper module.  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > truncate is what it's called. I was actually searching for 'rails StringHelper'... 🙂
> 
> Thanks for the replies!  
> &nbsp;&nbsp;  
> Also:

a\_long\_string = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque pulvinar turpis a nisi. Cras id elit. Aliquam vitae pede nec lacus elementum lacinia. Ut aliquam ehicula sem."  
limit = 86  
a\_long\_string[0, limit] \<\< "..." #=\> "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque pulvinar turpis..."

-Justin

---

<div class="post-metadata">

**Author:** ![Shot\_Piotr\_Szotkowsk](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@Shot\_Piotr\_Szotkowsk](https://rubytalk.org/u/Shot_Piotr_Szotkowsk)\
**Post date:** [11 November 2008 12:22 UTC](https://rubytalk.org/t/what-is-that-method-called/50127/7 "2008-11-11T12:22:33Z")

</div>

Justin Collins:

> a\_long\_string = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.  
> Pellentesque pulvinar turpis a nisi. Cras id elit. Aliquam vitae pede nec  
> lacus elementum lacinia. Ut aliquam ehicula sem."  
> limit = 86  
> a\_long\_string[0, limit] \<\< "..." #=\> "Lorem ipsum dolor sit amet,  
> consectetuer adipiscing elit. Pellentesque pulvinar turpis..."

Note that in Ruby 1.8 String# doesn’t work on chars, but bytes,  
which means the above is not reliable for multibyte strings like UTF-8:

> > polish = 'W trosce o byt i przyszłość naszej Ojczyzny'

=\> "W trosce o byt i przyszłość naszej Ojczyzny"

> > polish[0, 27]

=\> "W trosce o byt i przyszło\305"

> > polish[0, 30]

=\> "W trosce o byt i przyszłość"

In Ruby 1.9 String# works on chars, so the above works as expected:

> > polish = 'W trosce o byt i przyszłość naszej Ojczyzny'

=\> "W trosce o byt i przyszłość naszej Ojczyzny"

> > polish[0, 27]

=\> "W trosce o byt i przyszłość"

-- Shot

> **···**
>
> --  
> Making the choice to roll your own always is a tough one,  
> because it breaks Programmer's Rule Number 42, which clearly  
> states, "Every problem has been solved. It is Open Source.  
> And it is the first link on Google." -- Ara T. Howard

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [11 November 2008 17:52 UTC](https://rubytalk.org/t/what-is-that-method-called/50127/8 "2008-11-11T17:52:26Z")

</div>

Justin Collins \<justincollins@ucla.edu\> writes:

> [Note: parts of this message were removed to make it a legal post.]
> 
> David Trasbo wrote:
> 
> > Peter Szinek wrote:  
> > &nbsp;&nbsp;
> > 
> > > I seriously doubt this is part of standard Ruby.  
> > > &nbsp;&nbsp;&nbsp;&nbsp;  
> > > You're right, it's not a part of the Ruby standard library:
> > 
> > Clifford Heath wrote:  
> > &nbsp;&nbsp;
> > 
> > > There's no such method in Ruby. You might be thinking of the  
> > > truncate method that's part of ActionView's TextHelper module.  
> > > &nbsp;&nbsp;&nbsp;&nbsp;  
> > > truncate is what it's called. I was actually searching for 'rails  
> > > StringHelper'... 🙂
> > 
> > Thanks for the replies!  
> > &nbsp;&nbsp;  
> > Also:
> 
> a\_long\_string = "Lorem ipsum dolor sit amet, consectetuer adipiscing  
> elit. Pellentesque pulvinar turpis a nisi. Cras id elit. Aliquam vitae  
> pede nec lacus elementum lacinia. Ut aliquam ehicula sem."  
> limit = 86  
> a\_long\_string[0, limit] \<\< "..." #=\> "Lorem ipsum dolor sit amet,  
> consectetuer adipiscing elit. Pellentesque pulvinar turpis..."

In general, for input strings less than the limit, you wouldn't want "..."

class String  
&nbsp;&nbsp;# Return an ellided string no longer than total\_length  
&nbsp;&nbsp;# "hello".ellide(5) -\> "hello"  
&nbsp;&nbsp;# "hello there".ellide(8) -\> "hello..."  
&nbsp;&nbsp;def ellide total\_length  
&nbsp;&nbsp;&nbsp;&nbsp;if self.length \> total\_length  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise 'total\_length must be at least 3' if total\_length \< 3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self[0, total\_length - 3] + '...'  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

> **···**
>
> > -Justin
> 
> --  
> Brian Adkins  
> [http://www.lojic.com/](http://www.lojic.com/)
> 
> > **[Lojic Technologies Blog](https://blog.lojic.com//)**
> >
> > Lojic Technologies Blog

---

<div class="post-metadata">

**Author:** ![Boris\_Blaadh](https://avatars.discourse-cdn.com/v4/letter/b/7ba0ec/32.png) [@Boris\_Blaadh](https://rubytalk.org/u/Boris_Blaadh)\
**Post date:** [11 November 2008 14:29 UTC](https://rubytalk.org/t/what-is-that-method-called/50127/9 "2008-11-11T14:29:45Z")

</div>

perhaps this works?

string = \<\<END  
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque  
pulvinar turpis a nisi. Cras id elit. Aliquam vitae pede nec lacus  
elementum lacinia. Ut aliquam vehicula sem.  
END

lead = 90

wordlist = string.split(' ')

shorttext = ''

for word in wordlist

&nbsp;&nbsp;break if(shorttext.length + word.length \> lead)  
&nbsp;&nbsp;shorttext = shorttext + word + " "

end

# get rid of the last space  
shorttext.chop!

puts "#{shorttext}..."

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [11 November 2008 17:57 UTC](https://rubytalk.org/t/what-is-that-method-called/50127/10 "2008-11-11T17:57:27Z")

</div>

Boris Blaadh \<nejnejnej@gmail.com\> writes:

> perhaps this works?
> 
> string = \<\<END  
> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Pellentesque  
> pulvinar turpis a nisi. Cras id elit. Aliquam vitae pede nec lacus  
> elementum lacinia. Ut aliquam vehicula sem.  
> END
> 
> lead = 90
> 
> wordlist = string.split(' ')
> 
> shorttext = ''
> 
> for word in wordlist
> 
> &nbsp;&nbsp;break if(shorttext.length + word.length \> lead)  
> &nbsp;&nbsp;shorttext = shorttext + word + " "
> 
> end
> 
> # get rid of the last space  
> shorttext.chop!
> 
> puts "#{shorttext}..."

It is nicer to break at word boundaries in some contexts. But if it's  
a single word (such as a long email address), then you'd still want to  
show something more than "...". Maybe you can add a check for that.

e.g. for string = 'john.smith@hisdomain.com', lead = 15

> **···**
>
> --  
> Brian Adkins  
> [http://www.lojic.com/](http://www.lojic.com/)
> 
> > **[Lojic Technologies Blog](https://blog.lojic.com//)**
> >
> > Lojic Technologies Blog

---

<div class="post-metadata">

**Author:** ![Boris\_Blaadh](https://avatars.discourse-cdn.com/v4/letter/b/7ba0ec/32.png) [@Boris\_Blaadh](https://rubytalk.org/u/Boris_Blaadh)\
**Post date:** [12 November 2008 11:38 UTC](https://rubytalk.org/t/what-is-that-method-called/50127/11 "2008-11-12T11:38:52Z")

</div>

Brian Adkins wrote:

> Boris Blaadh \<nejnejnej@gmail.com\> writes:
> 
> > wordlist = string.split(' ')  
> > # get rid of the last space  
> > shorttext.chop!
> > 
> > puts "#{shorttext}..."
> 
> It is nicer to break at word boundaries in some contexts. But if it's  
> a single word (such as a long email address), then you'd still want to  
> show something more than "...". Maybe you can add a check for that.
> 
> e.g. for string = 'john.smith@hisdomain.com', lead = 15

Hehe, ok:

if shorttext.empty?  
&nbsp;&nbsp;puts wordlist[0]  
else  
&nbsp;&nbsp;puts "#{shorttext}..."  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [12 November 2008 20:52 UTC](https://rubytalk.org/t/what-is-that-method-called/50127/12 "2008-11-12T20:52:28Z")

</div>

Boris Blaadh \<nejnejnej@gmail.com\> writes:

> Brian Adkins wrote:
> 
> > Boris Blaadh \<nejnejnej@gmail.com\> writes:
> > 
> > > wordlist = string.split(' ')  
> > > # get rid of the last space  
> > > shorttext.chop!
> > > 
> > > puts "#{shorttext}..."
> > 
> > It is nicer to break at word boundaries in some contexts. But if it's  
> > a single word (such as a long email address), then you'd still want to  
> > show something more than "...". Maybe you can add a check for that.
> > 
> > e.g. for string = 'john.smith@hisdomain.com', lead = 15
> 
> Hehe, ok:
> 
> if shorttext.empty?  
> &nbsp;&nbsp;puts wordlist[0]  
> else  
> &nbsp;&nbsp;puts "#{shorttext}..."  
> end

That fixes the problem of showing only '...', but now 'puts  
wordlist[0]' may display more than the limit.

Refining your idea a bit gave the following:

&nbsp;&nbsp;def elide\_words total\_length  
&nbsp;&nbsp;&nbsp;&nbsp;return self unless self.length \> total\_length  
&nbsp;&nbsp;&nbsp;&nbsp;raise 'total\_length must be at least 3' if total\_length \< 3  
&nbsp;&nbsp;&nbsp;&nbsp;words = self.split(' ')  
&nbsp;&nbsp;&nbsp;&nbsp;result = ''  
&nbsp;&nbsp;&nbsp;&nbsp;words.each do |word|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;len = total\_length - (result.empty? ? 3 : 4)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break if result.length + word.length \> len  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result \<\< ' ' unless result.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result \<\< word  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;if result.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self.elide(total\_length)  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return result + '...'  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

But we really don't need to find \*all\* the space boundaries via  
split(), so the following version is about 10 times faster:

&nbsp;&nbsp;def elide\_words total\_length  
&nbsp;&nbsp;&nbsp;&nbsp;return self unless self.length \> total\_length  
&nbsp;&nbsp;&nbsp;&nbsp;raise 'total\_length must be at least 3' if total\_length \< 3  
&nbsp;&nbsp;&nbsp;&nbsp;len = total\_length - 3 # room for '...'

&nbsp;&nbsp;&nbsp;&nbsp;# Case 1: natural word break  
&nbsp;&nbsp;&nbsp;&nbsp;return self[0, len] + '...' if self[len,1] == ' '

&nbsp;&nbsp;&nbsp;&nbsp;# Case 2: search backward for a char preceding a delimiter  
&nbsp;&nbsp;&nbsp;&nbsp;delim\_found = false  
&nbsp;&nbsp;&nbsp;&nbsp;idx = len.downto(0) {|i|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if delim\_found  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break (i + 1) if self[i,1] != ' '  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delim\_found = true if self[i,1] == ' '  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;return self[0, idx] + '...'  
&nbsp;&nbsp;end

> **···**
>
> --  
> Brian Adkins  
> [http://www.lojic.com/](http://www.lojic.com/)
> 
> > **[Lojic Technologies Blog](https://blog.lojic.com//)**
> >
> > Lojic Technologies Blog
