Join with o as a file path.
"merb"/"core_ext" #=> "merb/core_ext"
@param [String] o Path component to join with receiver.
@return [String] Receiver joined with o as a file path.
@api public
# File lib/extlib/string.rb, line 93 93: def /(o) 94: File.join(self, o.to_s) 95: end
Strips out whitespace then tests if the string is empty.
"".blank? #=> true " ".blank? #=> true " hey ho ".blank? #=> false
@return [TrueClass, FalseClass]
@api public
# File lib/extlib/blank.rb, line 86 86: def blank? 87: strip.empty? 88: end
Convert to camel case.
"foo_bar".camel_case #=> "FooBar"
@return [String] Receiver converted to camel case.
@api public
# File lib/extlib/string.rb, line 53 53: def camel_case 54: return self if self !~ /_/ && self =~ /[A-Z]+.*/ 55: split('_').map{|e| e.capitalize}.join 56: end
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"
@param [TrueClass, FalseClass] spaced (default=true)
Determines whether returned string has whitespace collapsed or removed
@return [String] Receiver with whitespace (including newlines) replaced
@api public
# File lib/extlib/string.rb, line 135 135: def compress_lines(spaced = true) 136: split($/).map { |line| line.strip }.join(spaced ? ' ' : '') 137: end
Escape all regexp special characters.
"*?{}.".escape_regexp #=> "\\*\\?\\{\\}\\."
@return [String] Receiver with all regexp special characters escaped.
@api public
# File lib/extlib/string.rb, line 12 12: def escape_regexp 13: Regexp.escape self 14: end
Remove whitespace margin.
@param [Object] indicator ???
@return [String] receiver with whitespace margin removed
@api public
# File lib/extlib/string.rb, line 147 147: def margin(indicator = nil) 148: lines = self.dup.split($/) 149: 150: min_margin = 0 151: lines.each do |line| 152: if line =~ /^(\s+)/ && (min_margin == 0 || $1.size < min_margin) 153: min_margin = $1.size 154: end 155: end 156: lines.map { |line| line.sub(/^\s{#{min_margin}}/, '') }.join($/) 157: end
# File lib/extlib/inflection.rb, line 438 438: def plural 439: Extlib::Inflection.plural(self) 440: end
Calculate a relative path from other.
"/opt/local/lib".relative_path_from("/opt/local/lib/ruby/site_ruby") # => "../.."
@param [String] other Base path to calculate from.
@return [String] Relative path from other to receiver.
@api public
# File lib/extlib/string.rb, line 107 107: def relative_path_from(other) 108: Pathname.new(self).relative_path_from(Pathname.new(other)).to_s 109: end
# File lib/extlib/inflection.rb, line 434 434: def singular 435: Extlib::Inflection.singular(self) 436: end
Convert to snake case.
"FooBar".snake_case #=> "foo_bar" "HeadlineCNNNews".snake_case #=> "headline_cnn_news" "CNN".snake_case #=> "cnn"
@return [String] Receiver converted to snake case.
@api public
# File lib/extlib/string.rb, line 38 38: def snake_case 39: return downcase if match(/\A[A-Z]+\z/) 40: gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). 41: gsub(/([a-z])([A-Z])/, '\1_\2'). 42: downcase 43: end
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"
@param [#] values
A list of values to translate and interpolate into receiver
@return [String]
Receiver translated with values translated and interpolated positionally
@api public
# File lib/extlib/string.rb, line 173 173: def t(*values) 174: self.class::translate(self) % values.collect! { |value| value.frozen? ? value : self.class::translate(value.to_s) } 175: end
Convert a constant name to a path, assuming a conventional structure.
"FooBar::Baz".to_const_path # => "foo_bar/baz"
@return [String] Path to the file containing the constant named by receiver
(constantized string), assuming a conventional structure.
@api public
# File lib/extlib/string.rb, line 79 79: def to_const_path 80: snake_case.gsub(/::/, "/") 81: end
Convert a path string to a constant name.
"merb/core_ext/string".to_const_string #=> "Merb::CoreExt::String"
@return [String] Receiver converted to a constant name.
@api public
# File lib/extlib/string.rb, line 66 66: def to_const_string 67: gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } 68: end
Unescape all regexp special characters.
"\\*\\?\\{\\}\\.".unescape_regexp #=> "*?{}."
@return [String] Receiver with all regexp special characters unescaped.
@api public
# File lib/extlib/string.rb, line 24 24: def unescape_regexp 25: self.gsub(/\\([\.\?\|\(\)\[\]\{\}\^\$\*\+\-])/, '\1') 26: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.