Substring with "..."

  • Thread starter Jose Antonio Parra
  • Start date
J

Jose Antonio Parra

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.
 
R

Rupert Voelcker

2008/12/2 Jose Antonio Parra said:
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
 
J

Jim Menard

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
 
J

Jose Antonio Parra

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.
 
J

Jim Menard

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
 
N

Neko

Jim said:
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.
 
R

Robert Klemme

2008/12/2 Jose Antonio Parra said:
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]

;-)

Kind regards

robert
 
T

Tim Pease

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.


$ 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top