Class: String

Inherits:
Object show all
Defined in:
lib/backports/1.9/string.rb,
lib/backports/1.8.7/string.rb,
lib/backports/rails/string.rb

Instance Method Summary collapse

Instance Method Details

#ascii_only?Boolean

Standard in Ruby 1.9. See official documentation

Returns:

  • (Boolean)


11
12
13
# File 'lib/backports/1.9/string.rb', line 11

def ascii_only?
  !(self =~ /[^\x00-\x7f]/)
end

#camelize(first_letter = :upper) ⇒ Object

Standard in rails. See official documentation



3
4
5
6
7
8
9
# File 'lib/backports/rails/string.rb', line 3

def camelize(first_letter = :upper) 
  if first_letter == :upper
    gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  else
    first.downcase + camelize[1..-1]
  end
end

#chrObject

Standard in Ruby 1.8.7+. See official documentation



6
7
8
# File 'lib/backports/1.8.7/string.rb', line 6

def chr
  chars.first
end

#clearObject

Standard in Ruby 1.8.7+. See official documentation



16
17
18
19
# File 'lib/backports/1.9/string.rb', line 16

def clear
  self[0,length] = ""
  self
end

#codepointsObject Also known as: each_codepoint

Standard in Ruby 1.9. See official documentation



22
23
24
25
26
27
28
29
# File 'lib/backports/1.9/string.rb', line 22

def codepoints
  return to_enum(:codepoints) unless block_given?
  each_char.each do |s|
    utf8 = s.each_byte.to_a
    utf8[0] &= 0xff >> utf8.size # clear high bits (1 to 4, depending of number of bytes used)
    yield utf8.inject{|result, b| (result << 6) + (b & 0x3f) }
  end
end

#constantizeObject

Standard in rails. See official documentation



12
13
14
15
16
17
18
19
20
21
# 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

#dasherizeObject

Standard in rails. See official documentation



24
25
26
# File 'lib/backports/rails/string.rb', line 24

def dasherize
  gsub(/_/, '-')
end

#demodulizeObject

Standard in rails. See official documentation



29
30
31
# File 'lib/backports/rails/string.rb', line 29

def demodulize
  gsub(/^.*::/, '')
end

#each_char(&block) ⇒ Object Also known as: chars



20
21
22
23
# File 'lib/backports/1.8.7/string.rb', line 20

def each_char(&block)
  return to_enum(:each_char) unless block_given?
  scan(/./, &block)
end

#end_with?(*suffixes) ⇒ Boolean

Standard in Ruby 1.8.7+. See official documentation

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
# File 'lib/backports/1.8.7/string.rb', line 31

def end_with?(*suffixes)
  suffixes.each do |suffix|
    next unless suffix.respond_to? :to_str
    suffix = suffix.to_str
    return true if self[-suffix.length, suffix.length] == suffix
  end
  false
end

#getbyte(i) ⇒ Object

Standard in Ruby 1.9. See official documentation



35
36
37
# File 'lib/backports/1.9/string.rb', line 35

def getbyte(i)
  self[i]
end

#partition_with_new_meaning(pattern = Backports::Undefined, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/backports/1.8.7/string.rb', line 42

def partition_with_new_meaning(pattern = Backports::Undefined, &block)
  return partition_without_new_meaning(&block) 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

#rpartition(pattern) ⇒ Object

Standard in Ruby 1.8.7+. See official documentation



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/backports/1.8.7/string.rb', line 59

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) ⇒ Boolean

Standard in Ruby 1.8.7+. See official documentation

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
# File 'lib/backports/1.8.7/string.rb', line 74

def start_with?(*prefixes)
  prefixes.each do |prefix|
    next unless prefix.respond_to? :to_str
    prefix = prefix.to_str
    return true if self[0, prefix.length] == prefix
  end
  false
end

#try_convert(x) ⇒ Object

Standard in Ruby 1.9. See official documentation



4
5
6
7
# File 'lib/backports/1.9/string.rb', line 4

def try_convert(x)
  return nil unless x.respond_do(:to_str)
  x.to_str
end

#underscoreObject

Standard in rails. See official documentation



34
35
36
37
38
39
40
# 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, &block) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/backports/1.8.7/string.rb', line 85

def upto_with_exclusive(to, excl=false, &block)
  return upto_without_exclusive(to, &block) if block_given? && !excl
  enum = Range.new(self, to, excl).to_enum
  return enum unless block_given?
  enum.each(&block)
  self
end