Class: String

Inherits:
Object show all
Defined in:
lib/ruco/core_ext/string.rb,
lib/ruco/core_ext/string.rb

Instance Method Summary collapse

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(options={})
  max = options[:max] || 40
  delimiter = options[: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_whitespaceObject



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

#ordObject



26
27
28
# File 'lib/ruco/core_ext/string.rb', line 26

def ord
  bytes.first
end

#surrounded_in?(*words) ⇒ Boolean

Returns:

  • (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

#tabs_to_spaces!Object



6
7
8
# File 'lib/ruco/core_ext/string.rb', line 6

def tabs_to_spaces!
  gsub!("\t",' ' * Ruco::TAB_SIZE)
end