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: >> >>> 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! >> > > 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 # Return an ellided string no longer than total_length # "hello".ellide(5) -> "hello" # "hello there".ellide(8) -> "hello..." def ellide total_length if self.length > total_length raise 'total_length must be at least 3' if total_length < 3 return self[0, total_length - 3] + '...' else return self end end end > > -Justin > -- Brian Adkins http://www.lojic.com/ http://lojic.com/blog/