class String
Public Instance Methods
Strips out whitespace then tests if the string is empty.
"".blank? #=> true " ".blank? #=> true " hey ho ".blank? #=> false
@return [TrueClass, FalseClass]
# File lib/gorillib/object/blank.rb, line 93 def blank? strip.empty? end
# File lib/gorillib/string/inflections.rb, line 4 def camelize(*args) Gorillib::Inflector.camelize(self, *args) ; end
Find a declared constant with the name specified in the string, or raise.
@example
"Module".constantize # => Module "Class".constantize # => Class "blargle".constantize # => NameError: wrong constant name blargle
@raise [NameError] when the name is not in CamelCase or is not initialized. @return [Module,Class] the specified class @see Gorillib::Inflector.constantize
# File lib/gorillib/string/constantize.rb, line 14 def constantize Gorillib::Inflector.constantize(self) end
# File lib/gorillib/string/inflections.rb, line 7 def demodulize(*args) Gorillib::Inflector.demodulize(self, *args) ; end
Capitalizes the first word and turns underscores into spaces and strips a trailing “_id”, if any. Like titleize
, this is meant for creating pretty output.
@example
"employee_salary" #=> "Employee salary" "author_id" #=> "Author"
# File lib/gorillib/string/human.rb, line 12 def humanize self.gsub(/_id$/, '').tr('_', ' ').capitalize end
Find a declared constant with the name specified in the string, or return nil.
@return [Module,Class] the specified class, or nil when the name is not in CamelCase or is not initialized.
@example
"Module".safe_constantize # => Module "Class".safe_constantize # => Class "blargle".safe_constantize # => nil
@see Gorillib::Model::Inflector.safe_constantize @return [Module,Class] the specified constant,
or nil when the name is not in CamelCase or is not initialized.
# File lib/gorillib/string/constantize.rb, line 31 def safe_constantize Gorillib::Inflector.safe_constantize(self) end
# File lib/gorillib/string/inflections.rb, line 5 def snakeize(*args) Gorillib::Inflector.snakeize(self, *args) ; end
Capitalizes all the words and replaces some characters in the string to create a nicer looking title. titleize
is meant for creating pretty output. It is not used in the Rails internals.
Examples:
"man from the boondocks".titleize # => "Man From The Boondocks" "x-men: the last stand".titleize # => "X Men: The Last Stand"
# File lib/gorillib/string/human.rb, line 23 def titleize self.underscore.humanize.gsub(/\b('?[a-z])/){ $1.capitalize } end
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..."
The last characters will be replaced with the :omission
string (defaults to “…”) for a total length not exceeding :length
:
"Once upon a time in a world far far away".truncate(27, :separator => ' ') # => "Once upon a time in a..."
Pass a :separator
to truncate text
at a natural break:
"And they found that many people were sleeping better.".truncate(25, :omission => "... (continued)") # => "And they f... (continued)"
# File lib/gorillib/string/truncate.rb, line 17 def truncate(length, options = {}) text = self.dup chars = text.respond_to?(:mb_chars) ? text.mb_chars : text omission = options[:omission] || "..." omission_len = omission.respond_to?(:mb_chars) ? omission.mb_chars.length : omission.length length_with_room_for_omission = length - omission_len if (separator = options[:separator]) separator_chars = separator.respond_to?(:mb_chars) ? separator.to_s.mb_chars : separator.to_s stop = chars.rindex(separator_chars, length_with_room_for_omission) || length_with_room_for_omission else stop = length_with_room_for_omission end (chars.length > length ? chars[0...stop] + omission : text).to_s end
# File lib/gorillib/string/inflections.rb, line 6 def underscore(*args) Gorillib::Inflector.underscore(self, *args) ; end