Class: String

### Home Classes Methods

### In Files

- lib/extlib/blank.rb
- lib/extlib/inflection.rb
- lib/extlib/string.rb

### Parent

Object

### Methods

- ::translate
- ::translations
- #/
- #blank?
- #camel_case
- #compress_lines
- #escape_regexp
- #margin
- #plural
- #pluralize
- #relative_path_from
- #singular
- #singularize
- #snake_case
- #t
- #to_const_path
- #to_const_string
- #unescape_regexp

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- Extlib
- Extlib::Assertions
- Extlib::ByteArray
- Extlib::Hook
- Extlib::Hook::ClassMethods
- Extlib::Inflection
- Extlib::LocalObjectSpace
- Extlib::Logger
- Extlib::Pooling
- Extlib::Pooling::InvalidResourceError
- Extlib::Pooling::Pool
- Extlib::SimpleSet
- Gem
- Gem::SourceIndex
- Gem::Specification
- LazyModule
- LazyModule::ClassMethods
- Array
- Class
- DateTime
- Dictionary
- FalseClass
- Hash
- LazyArray
- Mash
- Module
- NilClass
- Numeric
- Object
- ObjectSpace
- Pathname
- REXMLUtilityNode
- String
- Struct
- Symbol
- Time
- ToHashParser
- TrueClass
- VirtualFile

No matching classes.

# String

### Public Class Methods

translate(value) click to toggle source

Overwrite this method to provide your own translations.

    # File lib/extlib/string.rb, line 112
    def self.translate(value)
      translations[value] || value
    end

translations() click to toggle source

    # File lib/extlib/string.rb, line 116
    def self.translations
      @translations ||= {}
    end

### Public Instance Methods

/(o) click to toggle source

Join with o as a file path.

    "merb"/"core_ext" #=> "merb/core_ext"

@param [String] o Path component to join with receiver.

@return [String] Receiver joined with o as a file path.

@api public

    # File lib/extlib/string.rb, line 93
    def /(o)
      File.join(self, o.to_s)
    end

blank?() click to toggle source

Strips out whitespace then tests if the string is empty.

    "".blank?         #=>  true
    "     ".blank?    #=>  true
    " hey ho ".blank? #=>  false

@return [TrueClass, FalseClass]

@api public

    # File lib/extlib/blank.rb, line 86
    def blank?
      strip.empty?
    end

camel_case() click to toggle source

Convert to camel case.

    "foo_bar".camel_case          #=> "FooBar"

@return [String] Receiver converted to camel case.

@api public

    # File lib/extlib/string.rb, line 53
    def camel_case
      return self if self !~ /_/ && self =~ /[A-Z]+.*/
      split('_').map{|e| e.capitalize}.join
    end

compress_lines(spaced = true) click to toggle source

Replace sequences of whitespace (including newlines) with either a single space or remove them entirely (according to param spaced)

    <<QUERY.compress_lines
      SELECT name
      FROM users
    QUERY => "SELECT name FROM users"

@param [TrueClass, FalseClass] spaced (default=true)

    Determines whether returned string has whitespace collapsed or removed

@return [String] Receiver with whitespace (including newlines) replaced

@api public

    # File lib/extlib/string.rb, line 135
    def compress_lines(spaced = true)
      split($/).map { |line| line.strip }.join(spaced ? ' ' : '')
    end

escape_regexp() click to toggle source

Escape all regexp special characters.

    "*?{}.".escape_regexp   #=> "\\*\\?\\{\\}\\."

@return [String] Receiver with all regexp special characters escaped.

@api public

    # File lib/extlib/string.rb, line 12
    def escape_regexp
      Regexp.escape self
    end

margin(indicator = nil) click to toggle source

Remove whitespace margin.

@param [Object] indicator ???

@return [String] receiver with whitespace margin removed

@api public

    # File lib/extlib/string.rb, line 147
    def margin(indicator = nil)
      lines = self.dup.split($/)

      min_margin = 0
      lines.each do |line|
        if line =~ /^(\s+)/ && (min_margin == 0 || $1.size < min_margin)
          min_margin = $1.size
        end
      end
      lines.map { |line| line.sub(/^\s{#{min_margin}}/, '') }.join($/)
    end

plural() click to toggle source

    # File lib/extlib/inflection.rb, line 438
    def plural
      Extlib::Inflection.plural(self)
    end

Also aliased as: pluralize
pluralize() click to toggle source
Alias for: plural
relative_path_from(other) click to toggle source

Calculate a relative path from other.

    "/opt/local/lib".relative_path_from("/opt/local/lib/ruby/site_ruby") # => "../.."

@param [String] other Base path to calculate from.

@return [String] Relative path from other to receiver.

@api public

    # File lib/extlib/string.rb, line 107
    def relative_path_from(other)
      Pathname.new(self).relative_path_from(Pathname.new(other)).to_s
    end

singular() click to toggle source

    # File lib/extlib/inflection.rb, line 434
    def singular
      Extlib::Inflection.singular(self)
    end

Also aliased as: singularize
singularize() click to toggle source
Alias for: singular
snake_case() click to toggle source

Convert to snake case.

    "FooBar".snake_case           #=> "foo_bar"
    "HeadlineCNNNews".snake_case  #=> "headline_cnn_news"
    "CNN".snake_case              #=> "cnn"

@return [String] Receiver converted to snake case.

@api public

    # File lib/extlib/string.rb, line 38
    def snake_case
      return downcase if match(/\A[A-Z]+\z/)
      gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
      gsub(/([a-z])([A-Z])/, '\1_\2').
      downcase
    end

t(*values) click to toggle source

Formats String for easy translation. Replaces an arbitrary number of values using numeric identifier replacement.

    "%s %s %s" % %w(one two three)        #=> "one two three"
    "%3$s %2$s %1$s" % %w(one two three)  #=> "three two one"

@param [to_s] values

    A list of values to translate and interpolate into receiver

@return [String]

    Receiver translated with values translated and interpolated positionally

@api public

    # File lib/extlib/string.rb, line 173
    def t(*values)
      self.class::translate(self) % values.collect! { |value| value.frozen? ? value : self.class::translate(value.to_s) }
    end

to_const_path() click to toggle source

Convert a constant name to a path, assuming a conventional structure.

    "FooBar::Baz".to_const_path # => "foo_bar/baz"

@return [String] Path to the file containing the constant named by receiver

    (constantized string), assuming a conventional structure.

@api public

    # File lib/extlib/string.rb, line 79
    def to_const_path
      snake_case.gsub(/::/, "/")
    end

to_const_string() click to toggle source

Convert a path string to a constant name.

    "merb/core_ext/string".to_const_string #=> "Merb::CoreExt::String"

@return [String] Receiver converted to a constant name.

@api public

    # File lib/extlib/string.rb, line 66
    def to_const_string
      gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
    end

unescape_regexp() click to toggle source

Unescape all regexp special characters.

    "\\*\\?\\{\\}\\.".unescape_regexp #=> "*?{}."

@return [String] Receiver with all regexp special characters unescaped.

@api public

    # File lib/extlib/string.rb, line 24
    def unescape_regexp
      self.gsub(/\\([\.\?\|\(\)\[\]\{\}\^\$\*\+\-])/, '\1')
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.