Class: String
- Defined in:
- lib/extlib/blank.rb,
lib/extlib/string.rb,
lib/extlib/inflection.rb
Overview
class FalseClass
Direct Known Subclasses
Class Method Summary collapse
-
.translate(value) ⇒ Object
Overwrite this method to provide your own translations.
- .translations ⇒ Object
Instance Method Summary collapse
-
#/(o) ⇒ String
Join with o as a file path.
-
#blank? ⇒ TrueClass, FalseClass
Strips out whitespace then tests if the string is empty.
-
#camel_case ⇒ String
Convert to camel case.
-
#compress_lines(spaced = true) ⇒ String
Replace sequences of whitespace (including newlines) with either a single space or remove them entirely (according to param spaced).
-
#escape_regexp ⇒ String
Escape all regexp special characters.
-
#margin(indicator = nil) ⇒ String
Remove whitespace margin.
- #plural ⇒ Object (also: #pluralize)
-
#relative_path_from(other) ⇒ String
Calculate a relative path from other.
- #singular ⇒ Object (also: #singularize)
-
#snake_case ⇒ String
Convert to snake case.
-
#t(*values) ⇒ String
Formats String for easy translation.
-
#to_const_path ⇒ String
Convert a constant name to a path, assuming a conventional structure.
-
#to_const_string ⇒ String
Convert a path string to a constant name.
-
#unescape_regexp ⇒ String
Unescape all regexp special characters.
Class Method Details
.translate(value) ⇒ Object
Overwrite this method to provide your own translations.
111 112 113 |
# File 'lib/extlib/string.rb', line 111 def self.translate(value) translations[value] || value end |
.translations ⇒ Object
115 116 117 |
# File 'lib/extlib/string.rb', line 115 def self.translations @translations ||= {} end |
Instance Method Details
#/(o) ⇒ String
Join with o as a file path.
"merb"/"core_ext" #=> "merb/core_ext"
92 93 94 |
# File 'lib/extlib/string.rb', line 92 def /(o) File.join(self, o.to_s) end |
#blank? ⇒ TrueClass, FalseClass
Strips out whitespace then tests if the string is empty.
"".blank? #=> true
" ".blank? #=> true
" hey ho ".blank? #=> false
86 87 88 |
# File 'lib/extlib/blank.rb', line 86 def blank? strip.empty? end |
#camel_case ⇒ String
Convert to camel case.
"foo_bar".camel_case #=> "FooBar"
52 53 54 55 |
# File 'lib/extlib/string.rb', line 52 def camel_case return self if self !~ /_/ && self =~ /[A-Z]+.*/ split('_').map{|e| e.capitalize}.join end |
#compress_lines(spaced = true) ⇒ String
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"
134 135 136 |
# File 'lib/extlib/string.rb', line 134 def compress_lines(spaced = true) split($/).map { |line| line.strip }.join(spaced ? ' ' : '') end |
#escape_regexp ⇒ String
Escape all regexp special characters.
"*?{}.".escape_regexp #=> "\\*\\?\\{\\}\\."
12 13 14 |
# File 'lib/extlib/string.rb', line 12 def escape_regexp Regexp.escape self end |
#margin(indicator = nil) ⇒ String
Remove whitespace margin.
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/extlib/string.rb', line 146 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 |
#plural ⇒ Object Also known as: pluralize
430 431 432 |
# File 'lib/extlib/inflection.rb', line 430 def plural Extlib::Inflection.plural(self) end |
#relative_path_from(other) ⇒ String
Calculate a relative path from other.
"/opt/local/lib".relative_path_from("/opt/local/lib/ruby/site_ruby") # => "../.."
106 107 108 |
# File 'lib/extlib/string.rb', line 106 def relative_path_from(other) Pathname.new(self).relative_path_from(Pathname.new(other)).to_s end |
#singular ⇒ Object Also known as: singularize
426 427 428 |
# File 'lib/extlib/inflection.rb', line 426 def singular Extlib::Inflection.singular(self) end |
#snake_case ⇒ String
Convert to snake case.
"FooBar".snake_case #=> "foo_bar"
"HeadlineCNNNews".snake_case #=> "headline_cnn_news"
"CNN".snake_case #=> "cnn"
38 39 40 41 42 |
# File 'lib/extlib/string.rb', line 38 def snake_case return self.downcase if self =~ /^[A-Z]+$/ self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/ return $+.downcase end |
#t(*values) ⇒ String
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"
172 173 174 |
# File 'lib/extlib/string.rb', line 172 def t(*values) self.class::translate(self) % values.collect! { |value| value.frozen? ? value : self.class::translate(value.to_s) } end |
#to_const_path ⇒ String
Convert a constant name to a path, assuming a conventional structure.
"FooBar::Baz".to_const_path # => "foo_bar/baz"
78 79 80 |
# File 'lib/extlib/string.rb', line 78 def to_const_path snake_case.gsub(/::/, "/") end |
#to_const_string ⇒ String
Convert a path string to a constant name.
"merb/core_ext/string".to_const_string #=> "Merb::CoreExt::String"
65 66 67 |
# File 'lib/extlib/string.rb', line 65 def to_const_string gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } end |
#unescape_regexp ⇒ String
Unescape all regexp special characters.
"\\*\\?\\{\\}\\.".unescape_regexp #=> "*?{}."
24 25 26 |
# File 'lib/extlib/string.rb', line 24 def unescape_regexp self.gsub(/\\([\.\?\|\(\)\[\]\{\}\^\$\*\+\-])/, '\1') end |