Class String
In: lib/backports/1.8.7/string.rb
lib/backports/1.9.3/string.rb
lib/backports/rails/string.rb
lib/backports/force/string_length.rb
lib/backports/1.9.1/string.rb
Parent: Object

encoding: utf-8

Methods

External Aliases

bytesize -> self
bytes -> self
lines -> self
each_codepoint -> self
getbyte -> self
setbyte -> self

Public Class methods

Standard in Ruby 1.8.8. See official documentation

[Source]

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

Public Instance methods

Standard in Ruby 1.9. See official documentation

[Source]

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

Standard in Ruby 1.9.3 See official documentation

[Source]

    # File lib/backports/1.9.3/string.rb, line 3
 3:   def byteslice(start, len = Backports::Undefined)
 4:     # Argument parsing & checking
 5:     if Backports::Undefined == len
 6:       if start.is_a?(Range)
 7:         range = start
 8:         start = Backports.coerce_to_int(range.begin)
 9:         start += bytesize if start < 0
10:         last = Backports.coerce_to_int(range.end)
11:         last += bytesize if last < 0
12:         last += 1 unless range.exclude_end?
13:         len = last - start
14:       else
15:         start = Backports.coerce_to_int(start)
16:         start += bytesize if start < 0
17:         len = 1
18:         return if start >= bytesize
19:       end
20:     else
21:       start = Backports.coerce_to_int(start)
22:       start += bytesize if start < 0
23:       len = Backports.coerce_to_int(len)
24:       return if len < 0
25:     end
26:     return if start < 0 || start > bytesize
27:     len = 0 if len < 0
28:     # Actual implementation:
29:     str = unpack("@#{start}a#{len}").first
30:     str = dup.replace(str) unless self.instance_of?(String) # Must return subclass
31:     str.force_encoding(encoding)
32:   end

Standard in rails. See official documentation

[Source]

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

Standard in Ruby 1.9. See official documentation

[Source]

    # File lib/backports/1.9.1/string.rb, line 16
16:   def chr
17:     chars.first || ""
18:   end

Standard in Ruby 1.9. See official documentation

[Source]

    # File lib/backports/1.9.1/string.rb, line 21
21:   def clear
22:     self[0,length] = ""
23:     self
24:   end

Standard in Ruby 1.9. See official documentation

[Source]

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

Standard in rails. See official documentation

[Source]

    # File lib/backports/rails/string.rb, line 12
12:   def constantize
13:     names = split('::')
14:     names.shift if names.empty? || names.first.empty?
15:   
16:     constant = Object
17:     names.each do |name|
18:       constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
19:     end
20:     constant
21:   end

Standard in rails. See official documentation

[Source]

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

Standard in rails. See official documentation

[Source]

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

[Source]

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

Standard in Ruby 1.8.7+. See official documentation

[Source]

    # File lib/backports/1.8.7/string.rb, line 29
29:   def end_with?(*suffixes)
30:     suffixes.any? do |suffix|
31:       if suffix.respond_to? :to_str
32:         suffix = suffix.to_str
33:         self[-suffix.length, suffix.length] == suffix
34:       end
35:     end
36:   end

[Source]

   # File lib/backports/force/string_length.rb, line 5
5:     def length
6:       unpack("U*").length
7:     end

Standard in Ruby 1.9. See official documentation

[Source]

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

[Source]

    # File lib/backports/1.8.7/string.rb, line 40
40:     def partition_with_new_meaning(pattern = Backports::Undefined)
41:       return partition_without_new_meaning{|c| yield c} if pattern == Backports::Undefined
42:       pattern = Backports.coerce_to(pattern, String, :to_str) unless pattern.is_a? Regexp
43:       i = index(pattern)
44:       return [self, "", ""] unless i
45:       if pattern.is_a? Regexp
46:         match = Regexp.last_match
47:         [match.pre_match, match[0], match.post_match]
48:       else
49:         last = i+pattern.length
50:         [self[0...i], self[i...last], self[last...length]]
51:       end
52:     end

Standard in Ruby 1.9.3 See official documentation

[Source]

    # File lib/backports/1.9.3/string.rb, line 35
35:   def prepend(other_str)
36:     replace Backports.coerce_to_str(other_str) + self
37:     self
38:   end

Standard in Ruby 1.8.7+. See official documentation

[Source]

    # File lib/backports/1.8.7/string.rb, line 57
57:   def rpartition(pattern)
58:     pattern = Backports.coerce_to(pattern, String, :to_str) unless pattern.is_a? Regexp
59:     i = rindex(pattern)
60:     return ["", "", self] unless i
61: 
62:     if pattern.is_a? Regexp
63:       match = Regexp.last_match
64:       [match.pre_match, match[0], match.post_match]
65:     else
66:       last = i+pattern.length
67:       [self[0...i], self[i...last], self[last...length]]
68:     end
69:   end

Standard in Ruby 1.8.7+. See official documentation

[Source]

    # File lib/backports/1.8.7/string.rb, line 72
72:   def start_with?(*prefixes)
73:     prefixes.any? do |prefix|
74:       if prefix.respond_to? :to_str
75:         prefix = prefix.to_str
76:         self[0, prefix.length] == prefix
77:       end
78:     end
79:   end

Standard in rails. See official documentation

[Source]

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

[Source]

    # File lib/backports/1.8.7/string.rb, line 83
83:     def upto_with_exclusive(to, excl=false)
84:       return upto_without_exclusive(to){|s| yield s} if block_given? && !excl
85:       enum = Range.new(self, to, excl).to_enum
86:       return enum unless block_given?
87:       enum.each{|s| yield s}
88:       self
89:     end

[Validate]