Class: String

Inherits:
Object show all
Includes:
Diff::LCS, Spec::Expectations::StringHelpers
Defined in:
lib/gems/builder-2.1.2/lib/builder/xchar.rb,
lib/gems/genosaurus-1.2.4/lib/genosaurus.rb,
lib/gems/logging-0.9.4/lib/logging/utils.rb,
lib/gems/diff-lcs-1.1.2/lib/diff/lcs/string.rb,
lib/gems/cucumber-0.1.15/lib/cucumber/core_ext/string.rb,
lib/gems/rspec-1.1.12/lib/spec/story/extensions/string.rb,
lib/gems/treetop-1.2.4/lib/treetop/ruby_extensions/string.rb,
lib/gems/configatron-2.2.2/lib/configatron/core_ext/string.rb,
lib/gems/rspec-1.1.12/lib/spec/expectations/extensions/string_and_symbol.rb

Overview

:nodoc:

Constant Summary

Constants included from Diff::LCS

Diff::LCS::BalancedCallbacks, Diff::LCS::PATCH_MAP, Diff::LCS::SequenceCallbacks, Diff::LCS::VERSION

Instance Method Summary collapse

Methods included from Spec::Expectations::StringHelpers

#starts_with?

Methods included from Diff::LCS

LCS, __diff_direction, __inverse_vector, __lcs, __normalize_patchset, __position_hash, __replace_next_larger, diff, #diff, #lcs, patch, #patch, #patch!, patch!, sdiff, #sdiff, traverse_balanced, #traverse_balanced, traverse_sequences, #traverse_sequences, #unpatch, #unpatch!, unpatch!

Instance Method Details

#arg_regexpObject



6
7
8
# File 'lib/gems/rspec-1.1.12/lib/spec/story/extensions/string.rb', line 6

def arg_regexp
  ::Spec::Story::Step::PARAM_OR_GROUP_PATTERN
end

#blank?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/gems/treetop-1.2.4/lib/treetop/ruby_extensions/string.rb', line 17

def blank?
  self == ""
end

#column_of(index) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/gems/treetop-1.2.4/lib/treetop/ruby_extensions/string.rb', line 2

def column_of(index)
  return 1 if index == 0
  newline_index = rindex("\n", index - 1)
  if newline_index
    index - newline_index
  else
    index + 1
  end
end

#gzub(regexp, format = nil, &proc) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gems/cucumber-0.1.15/lib/cucumber/core_ext/string.rb', line 2

def gzub(regexp, format=nil, &proc)
  md = match(regexp)
  raise "#{self.inspect} doesn't match #{regexp.inspect}" if md.nil?
  
  s = dup
  pos = 0
  md.captures.each_with_index do |m, n|
    replacement = if block_given?
      proc.call(m)
    else
      format % m
    end
    
    if md.offset(n+1)[0]
      s[md.offset(n+1)[0] + pos, m.length] = replacement
      pos += replacement.length - m.length
    end
  end
  s
end

#indent(n) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/gems/treetop-1.2.4/lib/treetop/ruby_extensions/string.rb', line 31

def indent(n)
  if n >= 0
    gsub(/^/, ' ' * n)
  else
    gsub(/^ {0,#{-n}}/, "")
  end
end

#line_of(index) ⇒ Object



12
13
14
# File 'lib/gems/treetop-1.2.4/lib/treetop/ruby_extensions/string.rb', line 12

def line_of(index)
  self[0...index].count("\n") + 1
end

#methodizeObject

:nodoc:

Raises:

  • (NameError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gems/configatron-2.2.2/lib/configatron/core_ext/string.rb', line 11

def methodize # :nodoc:
  x = self
  
  # if we get down to a nil or an empty string raise an exception! 
  raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
  
  # get rid of the big stuff in the front/back
  x.strip!
  
  # if we get down to a nil or an empty string raise an exception! 
  raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
  
  x = x.underscore
  
  # get rid of spaces and make the _
  x.gsub!(' ', '_')
  # get rid of everything that isn't 'safe' a-z, 0-9, ?, !, =, _
  x.gsub!(/([^ a-zA-Z0-9\_\?\!\=]+)/n, '_')
  
  # if we get down to a nil or an empty string raise an exception! 
  raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
  
  # condense multiple 'safe' non a-z chars to just one.
  # ie. ___ becomes _ !!!! becomes ! etc...
  [' ', '_', '?', '!', "="].each do |c|
    x.squeeze!(c)
  end
  
  # if we get down to a nil or an empty string raise an exception! 
  raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
  
  #down case the whole thing
  x.downcase!
  
  # get rid of any characters at the beginning that aren't a-z
  while !x.match(/^[a-z]/)
    x.slice!(0)
    
    # if we get down to a nil or an empty string raise an exception! 
    raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
  end
  
  # let's trim this bad boy down a bit now that we've cleaned it up, somewhat.
  # we should do this before cleaning up the end character, because it's possible to end up with a 
  # bad char at the end if you trim too late.
  x = x[0..100] if x.length > 100
  
  # get rid of any characters at the end that aren't safe
  while !x.match(/[a-z0-9\?\!\=]$/)
    x.slice!(x.length - 1)
    # if we get down to a nil or an empty string raise an exception! 
    raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
  end
  
  # if we get down to a nil or an empty string raise an exception! 
  raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
  
  # let's get rid of characters that don't belong in the 'middle' of the method.
  orig_middle = x[1..(x.length - 2)]
  n_middle = orig_middle.dup
  
  ['?', '!', "="].each do |c|
    n_middle.gsub!(c, "_")
  end
  
  # the previous gsub can leave us with multiple underscores that need cleaning up.
  n_middle.squeeze!("_")
  
  x.gsub!(orig_middle, n_middle)
  x.gsub!("_=", "=")
  x
end

#reduce(width, ellipses = '...') ⇒ Object

call-seq:

reduce( width, ellipses = '...' )    #=> string

Reduce the size of the current string to the given width by removing characters from the middle of the string and replacing them with ellipses. If the width is greater than the length of the string, the string is returned unchanged. If the width is less than the length of the ellipses, then the ellipses are returned.

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gems/logging-0.9.4/lib/logging/utils.rb', line 54

def reduce( width, ellipses = '...')
  raise ArgumentError, "width cannot be negative: #{width}" if width < 0

  return self if length <= width

  remove = length - width + ellipses.length
  return ellipses.dup if remove >= length

  left_end = (length + 1 - remove) / 2
  right_start = left_end + remove

  left = self[0,left_end]
  right = self[right_start,length-right_start]

  left << ellipses << right
end

#step_nameObject



2
3
4
# File 'lib/gems/rspec-1.1.12/lib/spec/story/extensions/string.rb', line 2

def step_name
  self
end

#tabto(n) ⇒ Object

The following methods are lifted from Facets 2.0.2



23
24
25
26
27
28
29
# File 'lib/gems/treetop-1.2.4/lib/treetop/ruby_extensions/string.rb', line 23

def tabto(n)
  if self =~ /^( *)\S/
    indent(n - $1.length)
  else
    self
  end
end

#to_xsObject

XML escaped version of to_s



110
111
112
113
114
# File 'lib/gems/builder-2.1.2/lib/builder/xchar.rb', line 110

def to_xs
  unpack('U*').map {|n| n.xchr}.join # ASCII, UTF-8
rescue
  unpack('C*').map {|n| n.xchr}.join # ISO-8859-1, WIN-1252
end

#treetop_camelizeObject



39
40
41
# File 'lib/gems/treetop-1.2.4/lib/treetop/ruby_extensions/string.rb', line 39

def treetop_camelize
  to_s.gsub(/\/(.?)/){ "::" + $1.upcase }.gsub(/(^|_)(.)/){ $2.upcase }
end

#underscoreObject

:nodoc:



3
4
5
6
7
8
9
10
# File 'lib/gems/configatron-2.2.2/lib/configatron/core_ext/string.rb', line 3

def underscore
  camel_cased_word = self.dup
  camel_cased_word.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end