Sha256: d1fd069f86d6eda03eddfe3dbd036b826ee54f1e7d6faf096588a24f5a944541

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

class String
  def indent spaces = 2
    indent = ' ' * spaces
    gsub /^/, indent
  end

  def rsplit *args
    reverse.split(*args).collect(&:reverse).reverse
  end

  def dirname
    File.expand_path(File.dirname(self))
  end

  def expand_path
    File.expand_path(self)
  end

  def to_a
    [self]
  end

  def underscore
    word = self.dup
    word.gsub!(/::/, '/')
    word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
    word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
    word.tr!("-", "_")
    word.downcase!
    word
  end

  def camelize first_letter_in_uppercase = true
    if first_letter_in_uppercase
      gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
    else
      self[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1]
    end
  end

  def constantize
    names = sub(/^::/, '').split '::'
    names.reduce(Object){|memo, name| memo.const_get name, false}
  end

  def substitute(*args)
    gsub(*args){yield Regexp.last_match.captures}
  end

  def substitute!(*args)
    gsub!(*args){yield Regexp.last_match.captures}
  end

  alias_method :blank?, :empty?
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby_ext-4.0.3 lib/ruby_ext/core/string.rb
ruby_ext-4.0.2 lib/ruby_ext/core/string.rb
ruby_ext-4.0.1 lib/ruby_ext/core/string.rb
ruby_ext-4.0.0 lib/ruby_ext/core/string.rb