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