Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/truncate.rb
Overview
truncate extends String to shorten text by character length or number of words.
Instance Method Summary collapse
-
#truncate(length = 20, omis = "...") ⇒ Object
The main “truncate” class can be passed a length (in number of characters) and / or a custom omission.
-
#words(length = 5, omis = "...") ⇒ Object
The “words” method can be passed a length (in number of words, splitting on “ ”) and / or a custom omission.
Instance Method Details
#truncate(length = 20, omis = "...") ⇒ Object
The main “truncate” class can be passed a length (in number of characters) and / or a custom omission. The default length is 20 characters and the default omission is “…”.
12 13 14 15 16 17 18 |
# File 'lib/truncate.rb', line 12 def truncate(length = 20, omis = "...") text = self.dup if text.length > length text = text[0...length].strip + omis end text end |
#words(length = 5, omis = "...") ⇒ Object
The “words” method can be passed a length (in number of words, splitting on “ ”) and / or a custom omission. The default length is 5 words and the default omission is “…”.
25 26 27 28 29 30 31 32 |
# File 'lib/truncate.rb', line 25 def words(length = 5, omis = "...") text = self.dup array = text.split(" ") if array.length > length text = array[0...length].join(" ") + omis end text end |