Substring with "..."


#1

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.


#2

On Tue, Dec 2, 2008 at 12:00 PM, Jose Antonio P.
removed_email_address@domain.invalid 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.

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


#3

2008/12/2 Jose Antonio P. removed_email_address@domain.invalid:

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.

It’s not actually part of the ruby api - it’s a rails function in
ActionView::Helpers::TextHelper -

truncate(text, *args)

Rupert


#4

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 M. 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
http://www.ruby-doc.org/core/classes/String.html

Jim


#5

On Tue, 02 Dec 2008 12:00:55 -0500, Jose Antonio P. 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.

Are you looking for http://rubyquiz.com/quiz83.html Short But Unique?


#6

On Tue, Dec 2, 2008 at 12:41 PM, Jose Antonio P.
removed_email_address@domain.invalid wrote:

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 M. 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
http://www.ruby-doc.org/core/classes/String.html

Jim


#7

2008/12/2 Jose Antonio P. removed_email_address@domain.invalid:

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.

There is a method in class String - kind of:

irb(main):009:0> w = [“a”, “b” * 17, “c” * 20, “d” * 25]
=> [“a”, “bbbbbbbbbbbbbbbbb”, “cccccccccccccccccccc”,
“ddddddddddddddddddddddddd”]
irb(main):010:0> x = w.map {|s| s.sub(%r{\A(.{17}).{4,}\z}, ‘\1…’)}
=> [“a”, “bbbbbbbbbbbbbbbbb”, “cccccccccccccccccccc”,
“ddddddddddddddddd…”]
irb(main):011:0> x.map {|s| s.length}
=> [1, 17, 20, 20]

:wink:

Kind regards

robert


#8

On Dec 2, 2008, at 10:00 AM, Jose Antonio P. 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/.

$ sudo gem install logging

$ cat reduce.rb

gem ‘logging’
require ‘logging’

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


#9

Jim M. wrote:

Is this a school assignment? What have you tried so far?

Well, since I’m a Ruby newbie, here’s my take (even if the thread author
was looking for a Rails solution):

phrase = gets.chomp

length = phrase.length

if length >= 20

  puts phrase.slice(0..20) + "..."

else

  puts phrase

end

What do you think?

Best regards,

-N.