E
E
eldar_web2015-12-01 18:18:59
ruby
eldar_web, 2015-12-01 18:18:59

How can you cut text in Ruby (after a certain number of characters)?

There is a long text. I want to cut out the rest of the text after 10 characters (and at the end I want to write '...').
How can I do that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
CapeRatel, 2015-12-01
@eldar_web

apidock.com/rails/ActionView/Helpers/TextHelper/tr... is the way to go (for rails).
In pure ruby, I think it's not a problem to write such a method yourself.

def truncate(len)
  return "#{self[0...len]}... " if self.length > len
  self
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question