Class: String

Inherits:
Object show all
Defined in:
lib/extlib/blank.rb,
lib/extlib/string.rb,
lib/extlib/inflection.rb

Overview

class FalseClass

Direct Known Subclasses

Extlib::ByteArray

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) translate(value)

Overwrite this method to provide your own translations.



112
113
114
# File 'lib/extlib/string.rb', line 112

def self.translate(value)
  translations[value] || value
end

+ (Object) translations



116
117
118
# File 'lib/extlib/string.rb', line 116

def self.translations
  @translations ||= {}
end

Instance Method Details

- (String) /(o)

Join with o as a file path.

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

Parameters:

  • o (String)

    Path component to join with receiver.

Returns:

  • (String)

    Receiver joined with o as a file path.



93
94
95
# File 'lib/extlib/string.rb', line 93

def /(o)
  File.join(self, o.to_s)
end

- (TrueClass, FalseClass) blank?

Strips out whitespace then tests if the string is empty.

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

Returns:



86
87
88
# File 'lib/extlib/blank.rb', line 86

def blank?
  strip.empty?
end

- (String) camel_case

Convert to camel case.

"foo_bar".camel_case          #=> "FooBar"

Returns:

  • (String)

    Receiver converted to camel case.



53
54
55
56
# File 'lib/extlib/string.rb', line 53

def camel_case
  return self if self !~ /_/ && self =~ /[A-Z]+.*/
  split('_').map{|e| e.capitalize}.join
end

- (String) compress_lines(spaced = true)

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"

Parameters:

  • spaced (TrueClass, FalseClass) (defaults to: true)

    (default=true) Determines whether returned string has whitespace collapsed or removed

Returns:

  • (String)

    Receiver with whitespace (including newlines) replaced



135
136
137
# File 'lib/extlib/string.rb', line 135

def compress_lines(spaced = true)
  split($/).map { |line| line.strip }.join(spaced ? ' ' : '')
end

- (String) escape_regexp

Escape all regexp special characters.

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

Returns:

  • (String)

    Receiver with all regexp special characters escaped.



12
13
14
# File 'lib/extlib/string.rb', line 12

def escape_regexp
  Regexp.escape self
end

- (String) margin(indicator = nil)

Remove whitespace margin.

Parameters:

  • indicator (Object) (defaults to: nil)

    ???

Returns:

  • (String)

    receiver with whitespace margin removed



147
148
149
150
151
152
153
154
155
156
157
# 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

- (Object) plural Also known as: pluralize



438
439
440
# File 'lib/extlib/inflection.rb', line 438

def plural
  Extlib::Inflection.plural(self)
end

- (String) relative_path_from(other)

Calculate a relative path from other.

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

Parameters:

  • other (String)

    Base path to calculate from.

Returns:

  • (String)

    Relative path from other to receiver.



107
108
109
# File 'lib/extlib/string.rb', line 107

def relative_path_from(other)
  Pathname.new(self).relative_path_from(Pathname.new(other)).to_s
end

- (Object) singular Also known as: singularize



434
435
436
# File 'lib/extlib/inflection.rb', line 434

def singular
  Extlib::Inflection.singular(self)
end

- (String) snake_case

Convert to snake case.

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

Returns:

  • (String)

    Receiver converted to snake case.



38
39
40
41
42
43
# 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

- (String) t(*values)

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"

Parameters:

  • values (#to_s)

    A list of values to translate and interpolate into receiver

Returns:

  • (String)

    Receiver translated with values translated and interpolated positionally



173
174
175
# 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

- (String) to_const_path

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

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

Returns:

  • (String)

    Path to the file containing the constant named by receiver (constantized string), assuming a conventional structure.



79
80
81
# File 'lib/extlib/string.rb', line 79

def to_const_path
  snake_case.gsub(/::/, "/")
end

- (String) to_const_string

Convert a path string to a constant name.

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

Returns:

  • (String)

    Receiver converted to a constant name.



66
67
68
# File 'lib/extlib/string.rb', line 66

def to_const_string
  gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end

- (String) unescape_regexp

Unescape all regexp special characters.

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

Returns:

  • (String)

    Receiver with all regexp special characters unescaped.



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

def unescape_regexp
  self.gsub(/\\([\.\?\|\(\)\[\]\{\}\^\$\*\+\-])/, '\1')
end