Class: String
- Defined in:
- lib/ruco/core_ext/string.rb,
lib/ruco/core_ext/string.rb
Instance Method Summary collapse
-
#ellipsize(options = {}) ⇒ Object
gist.github.com/20844 remove middle from strings exceeding max length.
- #force_encoding(encoding) ⇒ Object
- #indexes(needle) ⇒ Object
- #leading_whitespace ⇒ Object
- #leading_whitespace=(whitespace) ⇒ Object
- #naive_split(pattern) ⇒ Object
- #ord ⇒ Object
- #surrounded_in?(*words) ⇒ Boolean
- #tabs_to_spaces! ⇒ Object
Instance Method Details
#ellipsize(options = {}) ⇒ Object
gist.github.com/20844 remove middle from strings exceeding max length.
39 40 41 42 43 44 45 46 |
# File 'lib/ruco/core_ext/string.rb', line 39 def ellipsize(={}) max = [:max] || 40 delimiter = [:delimiter] || "..." return self if self.size <= max remainder = max - delimiter.size offset = remainder / 2 (self[0,offset + (remainder.odd? ? 1 : 0)].to_s + delimiter + self[-offset,offset].to_s)[0,max].to_s end |
#force_encoding(encoding) ⇒ Object
20 21 22 |
# File 'lib/ruco/core_ext/string.rb', line 20 def force_encoding(encoding) self end |
#indexes(needle) ⇒ Object
50 51 52 |
# File 'lib/ruco/core_ext/string.rb', line 50 def indexes(needle) Dispel::Tools.indexes(self, needle) end |
#leading_whitespace ⇒ Object
10 11 12 |
# File 'lib/ruco/core_ext/string.rb', line 10 def leading_whitespace match(/^\s*/)[0] end |
#leading_whitespace=(whitespace) ⇒ Object
14 15 16 |
# File 'lib/ruco/core_ext/string.rb', line 14 def leading_whitespace=(whitespace) sub!(/^\s*/, whitespace) end |
#naive_split(pattern) ⇒ Object
2 3 4 |
# File 'lib/ruco/core_ext/string.rb', line 2 def naive_split(pattern) Dispel::Tools.naive_split(self, pattern) end |
#ord ⇒ Object
26 27 28 |
# File 'lib/ruco/core_ext/string.rb', line 26 def ord bytes.first end |
#surrounded_in?(*words) ⇒ Boolean
31 32 33 34 35 |
# File 'lib/ruco/core_ext/string.rb', line 31 def surrounded_in?(*words) first = words.first last = words.last slice(0,first.size) == first and slice(-last.size,last.size) == last end |