Class: String

### Home Classes Methods

### In Files

- lib/backports/1.8.7/string/each_char.rb
- lib/backports/1.8.7/string/end_with.rb
- lib/backports/1.8.7/string/partition.rb
- lib/backports/1.8.7/string/rpartition.rb
- lib/backports/1.8.7/string/start_with.rb
- lib/backports/1.8.7/string/upto.rb
- lib/backports/1.9.1/string/ascii_only.rb
- lib/backports/1.9.1/string/chr.rb
- lib/backports/1.9.1/string/clear.rb
- lib/backports/1.9.1/string/codepoints.rb
- lib/backports/1.9.1/string/ord.rb
- lib/backports/1.9.3/string/byteslice.rb
- lib/backports/1.9.3/string/prepend.rb
- lib/backports/rails/string.rb

### Parent

Object

### Methods

- ::try_convert
- #ascii_only?
- #byteslice
- #camelize
- #chr
- #clear
- #codepoints
- #constantize
- #dasherize
- #demodulize
- #each_char
- #end_with?
- #ord
- #partition_with_new_meaning
- #prepend
- #rpartition
- #start_with?
- #underscore
- #upto_with_exclusive

### Class/Module Index

Quicksearch

- Prime
- Prime::EratosthenesGenerator
- Prime::EratosthenesSieve
- Prime::Generator23
- Prime::OldCompatibility
- Prime::PseudoPrimeGenerator
- Prime::TrialDivision
- Prime::TrialDivisionGenerator
- Backports
- Backports::MOST_EXTREME_OBJECT_EVER
- Backports::StdLib
- Backports::StdLib::LoadedFeatures
- Enumerator
- Enumerator::GeneratorBP
- Enumerator::Lazy
- Enumerator::Yielder
- Matrix
- Matrix::EigenvalueDecomposition
- Matrix::LUPDecomposition
- Random
- Random::Implementation
- Random::MT19937
- Enumerable
- Enumerable::Enumerator
- File
- File::Constants
- ARGF
- Array
- BasicObject
- Binding
- Complex
- Dir
- ENV
- Fixnum
- Float
- GC
- Hash
- IO
- Integer
- Kernel
- Math
- Method
- Module
- NilClass
- Numeric
- Object
- ObjectSpace
- OpenStruct
- Proc
- Process
- Range
- Regexp
- Set
- StopIteration
- String
- Struct
- Symbol
- UnboundMethod

No matching classes.

# String

### Public Class Methods

try_convert(x) click to toggle source

    # File lib/backports/1.9.1/string/try_convert.rb, line 4
    def String.try_convert(x)
      Backports.try_convert(x, String, :to_str)
    end

### Public Instance Methods

ascii_only?() click to toggle source

    # File lib/backports/1.9.1/string/ascii_only.rb, line 3
    def ascii_only?
      !(self =~ /[^\x00-\x7f]/)
    end

byteslice(start, len = Backports::Undefined) click to toggle source

    # File lib/backports/1.9.3/string/byteslice.rb, line 5
    def byteslice(start, len = Backports::Undefined)
      # Argument parsing & checking
      if Backports::Undefined == len
        if start.is_a?(Range)
          range = start
          start = Backports.coerce_to_int(range.begin)
          start += bytesize if start < 0
          last = Backports.coerce_to_int(range.end)
          last += bytesize if last < 0
          last += 1 unless range.exclude_end?
          len = last - start
        else
          start = Backports.coerce_to_int(start)
          start += bytesize if start < 0
          len = 1
          return if start >= bytesize
        end
      else
        start = Backports.coerce_to_int(start)
        start += bytesize if start < 0
        len = Backports.coerce_to_int(len)
        return if len < 0
      end
      return if start < 0 || start > bytesize
      len = 0 if len < 0
      # Actual implementation:
      str = unpack("@#{start}a#{len}").first
      str = dup.replace(str) unless self.instance_of?(String) # Must return subclass
      str.force_encoding(encoding)
    end

camelize(first_letter = :upper) click to toggle source

Standard in rails. See official documentation

    # File lib/backports/rails/string.rb, line 3
    def camelize(first_letter = :upper)
      if first_letter == :upper
        gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
      else
        self[0..0].downcase + camelize[1..-1]
      end
    end

chr() click to toggle source

    # File lib/backports/1.9.1/string/chr.rb, line 3
    def chr
      chars.first || ""
    end

clear() click to toggle source

    # File lib/backports/1.9.1/string/clear.rb, line 3
    def clear
      self[0,length] = ""
      self
    end

codepoints() click to toggle source

    # File lib/backports/1.9.1/string/codepoints.rb, line 3
    def codepoints
      return to_enum(:codepoints) unless block_given?
      unpack("U*").each{|cp| yield cp}
    end

constantize() click to toggle source

Standard in rails. See official documentation

    # File lib/backports/rails/string.rb, line 12
    def constantize
      names = 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

dasherize() click to toggle source

Standard in rails. See official documentation

    # File lib/backports/rails/string.rb, line 24
    def dasherize
      gsub(/_/, '-')
    end

demodulize() click to toggle source

Standard in rails. See official documentation

    # File lib/backports/rails/string.rb, line 29
    def demodulize
      gsub(/^.*::/, '')
    end

each_char() click to toggle source

    # File lib/backports/1.8.7/string/each_char.rb, line 6
    def each_char
      return to_enum(:each_char) unless block_given?
      scan(/./) {|c| yield c}
    end

end_with?(*suffixes) click to toggle source

    # File lib/backports/1.8.7/string/end_with.rb, line 3
    def end_with?(*suffixes)
      suffixes.any? do |suffix|
        if suffix.respond_to? :to_str
          suffix = suffix.to_str
          self[-suffix.length, suffix.length] == suffix
        end
      end
    end

ord() click to toggle source

    # File lib/backports/1.9.1/string/ord.rb, line 3
    def ord
      codepoints.first or raise ArgumentError, "empty string"
    end

partition_with_new_meaning(pattern = Backports::Undefined) click to toggle source

    # File lib/backports/1.8.7/string/partition.rb, line 5
    def partition_with_new_meaning(pattern = Backports::Undefined)
      return partition_without_new_meaning{|c| yield c} if pattern == Backports::Undefined
      pattern = Backports.coerce_to(pattern, String, :to_str) unless pattern.is_a? Regexp
      i = index(pattern)
      return [self, "", ""] unless i
      if pattern.is_a? Regexp
        match = Regexp.last_match
        [match.pre_match, match[0], match.post_match]
      else
        last = i+pattern.length
        [self[0...i], self[i...last], self[last...length]]
      end
    end

prepend(other_str) click to toggle source

    # File lib/backports/1.9.3/string/prepend.rb, line 5
    def prepend(other_str)
      replace Backports.coerce_to_str(other_str) + self
      self
    end

rpartition(pattern) click to toggle source

    # File lib/backports/1.8.7/string/rpartition.rb, line 5
    def rpartition(pattern)
      pattern = Backports.coerce_to(pattern, String, :to_str) unless pattern.is_a? Regexp
      i = rindex(pattern)
      return ["", "", self] unless i

      if pattern.is_a? Regexp
        match = Regexp.last_match
        [match.pre_match, match[0], match.post_match]
      else
        last = i+pattern.length
        [self[0...i], self[i...last], self[last...length]]
      end
    end

start_with?(*prefixes) click to toggle source

    # File lib/backports/1.8.7/string/start_with.rb, line 3
    def start_with?(*prefixes)
      prefixes.any? do |prefix|
        if prefix.respond_to? :to_str
          prefix = prefix.to_str
          self[0, prefix.length] == prefix
        end
      end
    end

underscore() click to toggle source

Standard in rails. See official documentation

    # File lib/backports/rails/string.rb, line 34
    def underscore
      gsub(/::/, '/').
        gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
        gsub(/([a-z\d])([A-Z])/,'\1_\2').
        tr("-", "_").
        downcase
    end

upto_with_exclusive(to, excl=false) click to toggle source

    # File lib/backports/1.8.7/string/upto.rb, line 6
    def upto_with_exclusive(to, excl=false)
      return upto_without_exclusive(to){|s| yield s} if block_given? && !excl
      r = Range.new(self, to, excl)
      return r.to_enum unless block_given?
      r.each{|s| yield s}
      self
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.