Class: String
- Defined in:
- lib/ruco/core_ext/string.rb,
lib/ruco/core_ext/string.rb
Overview
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
grosser.it/2011/08/28/ruby-string-naive-split-because-split-is-to-clever/ “ ”.split(‘ ’) == [] “ ”.naive_split(‘ ’) == [”,”,”,”] “”.split(‘ ’) == [] “”.naive_split(‘ ’) == [”].
- #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.
46 47 48 49 50 51 52 53 |
# File 'lib/ruco/core_ext/string.rb', line 46 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
27 28 29 |
# File 'lib/ruco/core_ext/string.rb', line 27 def force_encoding(encoding) self end |
#indexes(needle) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/ruco/core_ext/string.rb', line 58 def indexes(needle) found = [] current_index = -1 while current_index = index(needle, current_index+1) found << current_index end found end |
#leading_whitespace ⇒ Object
17 18 19 |
# File 'lib/ruco/core_ext/string.rb', line 17 def leading_whitespace match(/^\s*/)[0] end |
#leading_whitespace=(whitespace) ⇒ Object
21 22 23 |
# File 'lib/ruco/core_ext/string.rb', line 21 def leading_whitespace=(whitespace) sub!(/^\s*/, whitespace) end |
#naive_split(pattern) ⇒ Object
grosser.it/2011/08/28/ruby-string-naive-split-because-split-is-to-clever/ “ ”.split(‘ ’) == [] “ ”.naive_split(‘ ’) == [”,”,”,”] “”.split(‘ ’) == [] “”.naive_split(‘ ’) == [”]
7 8 9 10 11 |
# File 'lib/ruco/core_ext/string.rb', line 7 def naive_split(pattern) pattern = /#{Regexp.escape(pattern)}/ unless pattern.is_a?(Regexp) result = split(pattern, -1) result.empty? ? [''] : result end |
#ord ⇒ Object
33 34 35 |
# File 'lib/ruco/core_ext/string.rb', line 33 def ord bytes.first end |
#surrounded_in?(*words) ⇒ Boolean
38 39 40 41 42 |
# File 'lib/ruco/core_ext/string.rb', line 38 def surrounded_in?(*words) first = words.first last = words.last slice(0,first.size) == first and slice(-last.size,last.size) == last end |