Sha256: 2b4893c26b9f34e3e78db20f4aa16220aa45b5a6065a5da992c1af95f57fbc0c

Contents?: true

Size: 1.66 KB

Versions: 5

Compression:

Stored size: 1.66 KB

Contents

String.class_eval do
  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_reader
  #   self.to_sym
  # end

  # def to_writer
  #   "#{self}=".to_sym
  # end

  # def to_iv
  #   "@#{self}"
  # end

  def to_a
    [self]
  end

  # def interpolate binding
  #   binding.must_be.a Binding
  #   return gsub(/\#\{.+?\}/) do |term|
  #     identifier = term.slice(2 .. term.size-2)
  #     binding.eval identifier
  #   end
  # end

  def self.secure_token
    original = [Time.now, (1..10).map{ rand.to_s }]
    Digest::SHA1.hexdigest(original.flatten.join('--'))
  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 = self.split('::')
    names.shift if names.empty? || names.first.empty?

    constant = Object
    names.each do |name|
      constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
    end
    constant
  end

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

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

  alias_method :blank?, :empty?
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ruby_ext-0.5.8 lib/ruby_ext/core/string.rb
ruby_ext-0.5.7 lib/ruby_ext/core/string.rb
ruby_ext-0.5.6 lib/ruby_ext/core/string.rb
ruby_ext-0.5.5 lib/ruby_ext/core/string.rb
ruby_ext-0.5.4 lib/ruby_ext/core/string.rb