Class: String
Instance Method Summary collapse
-
#camelize(first_letter_in_uppercase = true) ⇒ Object
By default,
camelize
converts strings to UpperCamelCase. -
#constantize ⇒ Object
:nodoc:.
- #squish ⇒ Object
-
#squish! ⇒ Object
Performs a destructive squish.
-
#truncate(truncate_at, options = {}) ⇒ Object
Truncates a given
text
after a givenlength
iftext
is longer thanlength
:. -
#underscore ⇒ Object
Makes an underscored, lowercase form from the expression in the string.
Instance Method Details
#camelize(first_letter_in_uppercase = true) ⇒ Object
By default, camelize
converts strings to UpperCamelCase. If the argument to camelize
is set to :lower
then camelize
produces lowerCamelCase.
camelize
will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces.
As a rule of thumb you can think of camelize
as the inverse of underscore
, though there are cases where that does not hold:
"SSLError".underscore.camelize # => "SslError"
14 15 16 17 18 19 20 21 |
# File 'lib/ext_core/string.rb', line 14 def camelize(first_letter_in_uppercase = true) lower_case_and_underscored_word = self if first_letter_in_uppercase lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } else lower_case_and_underscored_word.to_s[0].chr.downcase + camelize(lower_case_and_underscored_word)[1..-1] end end |
#constantize ⇒ Object
:nodoc:
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ext_core/string.rb', line 71 def constantize() camel_cased_word = self names = camel_cased_word.split('::') names.shift if names.empty? || names.first.empty? constant = Object names.each do |name| constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name) end constant end |
#squish ⇒ Object
97 98 99 |
# File 'lib/ext_core/string.rb', line 97 def squish dup.squish! end |
#squish! ⇒ Object
Performs a destructive squish. See String#squish.
102 103 104 105 106 107 |
# File 'lib/ext_core/string.rb', line 102 def squish! gsub!(/\A[[:space:]]+/, '') gsub!(/[[:space:]]+\z/, '') gsub!(/[[:space:]]+/, ' ') self end |
#truncate(truncate_at, options = {}) ⇒ Object
Truncates a given text
after a given length
if text
is longer than length
:
'Once upon a time in a world far far away'.truncate(27)
# => "Once upon a time in a wo..."
Pass a string or regexp :separator
to truncate text
at a natural break:
'Once upon a time in a world far far away'.truncate(27, separator: ' ')
# => "Once upon a time in a..."
'Once upon a time in a world far far away'.truncate(27, separator: /\s/)
# => "Once upon a time in a..."
The last characters will be replaced with the :omission
string (defaults to “…”) for a total length not exceeding length
:
'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)')
# => "And they f... (continued)"
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/ext_core/string.rb', line 127 def truncate(truncate_at, = {}) return dup unless length > truncate_at [:omission] ||= '...' length_with_room_for_omission = truncate_at - [:omission].length stop = \ if [:separator] rindex([:separator], length_with_room_for_omission) || length_with_room_for_omission else length_with_room_for_omission end "#{self[0...stop]}#{[:omission]}" end |
#underscore ⇒ Object
Makes an underscored, lowercase form from the expression in the string.
Changes ‘::’ to ‘/’ to convert namespaces to paths.
Examples:
"FooBar".underscore # => "foo_bar"
"FooBar::Errors".underscore # => foo_bar/errors
As a rule of thumb you can think of underscore
as the inverse of camelize
, though there are cases where that does not hold:
"SSLError".underscore.camelize # => "SslError"
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ext_core/string.rb', line 38 def underscore() camel_cased_word = self word = camel_cased_word.to_s.dup word.gsub!(/::/, '/') word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end |