I want to know what is the method to take a string and return a
substring with 20 characters and "..." if the length of the string is
greater, or the complete string if it's shorter than 20 characters.
Thanks all.
It's not actually part of the ruby api - it's a rails function in
ActionView::Helpers::TextHelper -
truncate(text, *args)
Rupert
···
2008/12/2 Jose Antonio Parra <josparbar@gmail.com>:
Hi!
I want to know what is the method to take a string and return a
substring with 20 characters and "..." if the length of the string is
greater, or the complete string if it's shorter than 20 characters.
Thanks all.
Is this a school assignment? What have you tried so far?
You can break this problem down into two parts: grabbing the first 20
characters, and adding the "...". For bonus points, you should handle
the case where the string is already less than 20 characters long,
which would require that you find out how long the string is in the
first place.
You should look at the documentation for the String class, which will
list all of the methods available to you. You can see the list by
typing "ri String" in the shell or visiting http://www.ruby-doc.org/core/classes/String.html
Jim
···
On Tue, Dec 2, 2008 at 12:00 PM, Jose Antonio Parra <josparbar@gmail.com> wrote:
Hi!
I want to know what is the method to take a string and return a
substring with 20 characters and "..." if the length of the string is
greater, or the complete string if it's shorter than 20 characters.
Thanks all.
On Tue, 02 Dec 2008 12:00:55 -0500, Jose Antonio Parra wrote:
Hi!
I want to know what is the method to take a string and return a
substring with 20 characters and "..." if the length of the string is
greater, or the complete string if it's shorter than 20 characters.
Thanks all.
--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology. http://www.iit.edu/~kbloom1/
str = "this is a really long string and it would take up too much room to print"
puts str
puts str.reduce(50)
puts str.reduce(40, '---')
puts str.reduce(40, '_____')
$ ruby reduce.rb
this is a really long string and it would take up too much room to print
this is a really long st... too much room to print
this is a really lo---much room to print
this is a really l_____uch room to print
So, String#reduce is a method I put in the logging gem to shorten string by pulling characters out of the middle. The default replacement is an ellipses, but you can give it any string of any length to stick in the middle.
Grab the logging source code and steal the method if you want to use it without using the logging gem.
Blessings,
TwP
···
On Dec 2, 2008, at 10:00 AM, Jose Antonio Parra wrote:
Hi!
I want to know what is the method to take a string and return a
substring with 20 characters and "..." if the length of the string is
greater, or the complete string if it's shorter than 20 characters.
Thanks all.
-- Posted via http://www.ruby-forum.com/\.
Thaks. The method is truncate. I used that but I couldn't remember the
name. Jim, I had alredy looked at the String class, but no method do
that.
Jim Menard wrote:
···
You should look at the documentation for the String class, which will
list all of the methods available to you. You can see the list by
typing "ri String" in the shell or visiting class String - RDoc Documentation
Thaks. The method is truncate. I used that but I couldn't remember the
name. Jim, I had alredy looked at the String class, but no method do
that.
You are correct, there aren't. I didn't know you were looking for a
Rails method.
Jim Menard wrote:
You should look at the documentation for the String class, which will
list all of the methods available to you. You can see the list by
typing "ri String" in the shell or visiting class String - RDoc Documentation
Jim
···
On Tue, Dec 2, 2008 at 12:41 PM, Jose Antonio Parra <josparbar@gmail.com> wrote:
--
Jim Menard, jimm@io.com, jim.menard@gmail.com http://www.io.com/~jimm/