Sha256: b1b0b93763660044a7d2060d5cde65f12d4b99a997ff10e7e70d72b530b78a3c

Contents?: true

Size: 1.12 KB

Versions: 7

Compression:

Stored size: 1.12 KB

Contents

class String
  def naive_split(pattern)
    Dispel::Tools.naive_split(self, pattern)
  end

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

  def leading_whitespace
    match(/^\s*/)[0]
  end

  def leading_whitespace=(whitespace)
    sub!(/^\s*/, whitespace)
  end

  # stub for 1.8
  unless method_defined?(:force_encoding)
    def force_encoding(encoding)
      self
    end
  end

  unless method_defined?(:ord)
    def ord
      bytes.first
    end
  end

  def surrounded_in?(*words)
    first = words.first
    last = words.last
    slice(0,first.size) == first and slice(-last.size,last.size) == last
  end

  # https://gist.github.com/20844
  # remove middle from strings exceeding max length.
  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 unless defined? ellipsize
end

class String
  def indexes(needle)
    Dispel::Tools.indexes(self, needle)
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ruco-0.4.0 lib/ruco/core_ext/string.rb
ruco-0.3.0 lib/ruco/core_ext/string.rb
ruco-0.2.23 lib/ruco/core_ext/string.rb
ruco-0.2.22 lib/ruco/core_ext/string.rb
ruco-0.2.21 lib/ruco/core_ext/string.rb
ruco-0.2.20 lib/ruco/core_ext/string.rb
ruco-0.2.19 lib/ruco/core_ext/string.rb