| Class | String |
| In: |
lib/extlib/blank.rb
lib/extlib/string.rb lib/extlib/inflection.rb |
| Parent: | Object |
Overwrite this method to provide your own translations.
# File lib/extlib/string.rb, line 111
111: def self.translate(value)
112: translations[value] || value
113: 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
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 134
134: def compress_lines(spaced = true)
135: split($/).map { |line| line.strip }.join(spaced ? ' ' : '')
136: end
Remove whitespace margin.
@param [Object] indicator ???
@return [String] receiver with whitespace margin removed
@api public
# File lib/extlib/string.rb, line 146
146: def margin(indicator = nil)
147: lines = self.dup.split($/)
148:
149: min_margin = 0
150: lines.each do |line|
151: if line =~ /^(\s+)/ && (min_margin == 0 || $1.size < min_margin)
152: min_margin = $1.size
153: end
154: end
155: lines.map { |line| line.sub(/^\s{#{min_margin}}/, '') }.join($/)
156: end
# File lib/extlib/inflection.rb, line 430
430: def plural
431: Extlib::Inflection.plural(self)
432: 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 106
106: def relative_path_from(other)
107: Pathname.new(self).relative_path_from(Pathname.new(other)).to_s
108: end
# File lib/extlib/inflection.rb, line 426
426: def singular
427: Extlib::Inflection.singular(self)
428: 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 self.downcase if self =~ /^[A-Z]+$/
40: self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
41: return $+.downcase
42: 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 [to_s] 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 172
172: def t(*values)
173: self.class::translate(self) % values.collect! { |value| value.frozen? ? value : self.class::translate(value.to_s) }
174: 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 78
78: def to_const_path
79: snake_case.gsub(/::/, "/")
80: 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 65
65: def to_const_string
66: gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
67: 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