Forum: Ruby Substring with "..."

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jose Antonio P. (Guest)
on 2008-12-02 19:06
 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.
Rupert V. (Guest)
on 2008-12-02 19:21
(Received via mailing list)
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
Jim M. (Guest)
on 2008-12-02 19:21
(Received via mailing list)
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
Jose Antonio P. (Guest)
on 2008-12-02 19:47
 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
Jim M. (Guest)
on 2008-12-02 20:16
(Received via mailing list)
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
Ken B. (Guest)
on 2008-12-03 03:11
(Received via mailing list)
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?
Neko (Guest)
on 2008-12-03 03:40
(Received via mailing list)
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.
Robert K. (Guest)
on 2008-12-03 12:12
(Received via mailing list)
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]

;-)

Kind regards

robert
Tim P. (Guest)
on 2008-12-03 23:04
(Received via mailing list)
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
This topic is locked and can not be replied to.