Class: Scribble::Support::Utilities
- Inherits:
-
Object
- Object
- Scribble::Support::Utilities
- Defined in:
- lib/scribble/support/utilities.rb
Class Method Summary collapse
-
.ordinalize(number) ⇒ Object
Ordinalize number.
-
.repeat(string, count) ⇒ Object
String repetition.
-
.to_sentence(strings, final_separator = ' or ') ⇒ Object
Array to sentence.
-
.truncate(string, on_words, length, omission) ⇒ Object
String truncation.
Class Method Details
.ordinalize(number) ⇒ Object
Ordinalize number
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/scribble/support/utilities.rb', line 34 def ordinalize number if (11..13).include?(number % 100) "#{number}th" else case number % 10 when 1; "#{number}st" when 2; "#{number}nd" when 3; "#{number}rd" else "#{number}th" end end end |
.repeat(string, count) ⇒ Object
String repetition
8 9 10 11 |
# File 'lib/scribble/support/utilities.rb', line 8 def repeat string, count raise Errors::UnlocatedArgument.new("Can't repeat string a negative number of times") if count < 0 string * count end |
.to_sentence(strings, final_separator = ' or ') ⇒ Object
Array to sentence
29 30 31 |
# File 'lib/scribble/support/utilities.rb', line 29 def to_sentence strings, final_separator = ' or ' [strings[0..-2].join(', '), strings[-1].to_s].reject(&:empty?).join(final_separator) end |
.truncate(string, on_words, length, omission) ⇒ Object
String truncation
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/scribble/support/utilities.rb', line 15 def truncate string, on_words, length, omission raise Errors::UnlocatedArgument.new("Can't truncate string with a negative length") if length < 0 truncated = if on_words string[/(\s*\S*){#{length}}/] else string[0, length] end if string != truncated then truncated + omission else string end end |