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.
98ee355266e55690050a20fd76ee8c67?d=identicon&s=25 Jose Antonio Parra (pikislabis)
on 2008-12-02 18: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.
Fb1fc6f2eccd067a19b298d89235bfe0?d=identicon&s=25 Rupert Voelcker (rupert)
on 2008-12-02 18:21
(Received via mailing list)
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.

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

truncate(text, *args)

Rupert
F8101d1b395ef58feb1d5c99bf9356df?d=identicon&s=25 Jim Menard (Guest)
on 2008-12-02 18:21
(Received via mailing list)
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.

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
98ee355266e55690050a20fd76ee8c67?d=identicon&s=25 Jose Antonio Parra (pikislabis)
on 2008-12-02 18: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 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
> http://www.ruby-doc.org/core/classes/String.html
>
> Jim
F8101d1b395ef58feb1d5c99bf9356df?d=identicon&s=25 Jim Menard (Guest)
on 2008-12-02 19:16
(Received via mailing list)
On Tue, Dec 2, 2008 at 12:41 PM, Jose Antonio Parra
<josparbar@gmail.com> 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 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
>> http://www.ruby-doc.org/core/classes/String.html

Jim
851acbab08553d1f7aa3eecad17f6aa9?d=identicon&s=25 Ken Bloom (Guest)
on 2008-12-03 02:11
(Received via mailing list)
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.

Are you looking for http://rubyquiz.com/quiz83.html Short But Unique?
12907a06a624157407fe8af303a6eeed?d=identicon&s=25 Neko (Guest)
on 2008-12-03 02:40
(Received via mailing list)
Jim Menard 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.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-12-03 11:12
(Received via mailing list)
2008/12/2 Jose Antonio Parra <josparbar@gmail.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.

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
4d5b5dd4e263d780a5dfe7ac8b8ac98c?d=identicon&s=25 Tim Pease (Guest)
on 2008-12-03 22:04
(Received via mailing list)
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/.
>


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