Class: String
- Includes:
- Crypt::Noise, Crypt::StringXor
- Defined in:
- lib/framework/unicode_normalize.rb,
lib/build/ExtendedString.rb,
lib/extensions/crypt/crypt/noise.rb,
lib/extensions/crypt/crypt/stringxor.rb,
lib/extensions/bigdecimal/bigdecimal/util.rb
Overview
BigDecimal extends the native String class to provide the #to_d method.
When you require BigDecimal in your application, this method will be available on String objects.
Class Method Summary collapse
-
.natcmp(str1, str2, ignoreCase = true) ⇒ Object
from natcmp gem Natural comaration based on numbers in strings.
Instance Method Summary collapse
- #alarm ⇒ Object
- #bg_black ⇒ Object
- #bg_blue ⇒ Object
- #bg_brown ⇒ Object
- #bg_cyan ⇒ Object
- #bg_gray ⇒ Object
- #bg_green ⇒ Object
- #bg_magenta ⇒ Object
- #bg_red ⇒ Object
- #black ⇒ Object
- #blue ⇒ Object
- #bold ⇒ Object
- #brown ⇒ Object
-
#camel_case(*separators) ⇒ Object
Converts a string to camelcase.
-
#camelize(first_letter = :upper) ⇒ Object
(also: #camelcase)
By default,
camelizeconverts strings to UpperCamelCase. - #capitalize_first ⇒ Object
-
#classify ⇒ Object
Create a class name from a plural table name like Rails does for table names to models.
-
#constantize ⇒ Object
constantizetries to find a declared constant with the name specified in the string. - #cyan ⇒ Object
-
#dasherize ⇒ Object
Replaces underscores with dashes in the string.
-
#deconstantize ⇒ Object
Removes the rightmost segment from the constant expression in the string.
-
#demodulize ⇒ Object
Removes the module part from the constant expression in the string.
-
#foreign_key(separate_class_name_and_id_with_underscore = true) ⇒ Object
Creates a foreign key name from a class name.
- #gray ⇒ Object
- #green ⇒ Object
-
#hard_wrap(width = 80) ⇒ Object
def titleize underscore.humanize.gsub(/b(‘?)/) { $1.capitalize } end.
-
#humanize(options = {}) ⇒ Object
Capitalizes the first word, turns underscores into spaces, and strips a trailing ‘_id’ if present.
- #info ⇒ Object
-
#int? ⇒ Boolean
Returns true if string contains integer value.
- #magenta ⇒ Object
-
#modulize ⇒ Object
Converts a string to module name representation.
-
#numeric? ⇒ Boolean
Returns true if string contains numeric value.
-
#pluralize(count = nil, locale = :en) ⇒ Object
Returns the plural form of the word in the string.
- #primary ⇒ Object
- #red ⇒ Object
- #reverse_color ⇒ Object
-
#safe_constantize ⇒ Object
safe_constantizetries to find a declared constant with the name specified in the string. -
#singularize(locale = :en) ⇒ Object
The reverse of
pluralize, returns the singular form of a word in a string. - #success ⇒ Object
-
#tableize ⇒ Object
Creates the name of a table like Rails does for models to table names.
-
#titleize ⇒ Object
(also: #titlecase)
Capitalizes all the words and replaces some characters in the string to create a nicer looking title.
-
#to_d ⇒ Object
call-seq: string.to_d -> bigdecimal.
- #underline ⇒ Object
-
#underscore ⇒ Object
The reverse of
camelize. -
#unicode_normalize(form = :nfc) ⇒ Object
Unicode Normalization.
-
#unicode_normalize!(form = :nfc) ⇒ Object
:call-seq: str.unicode_normalize!(form=:nfc).
-
#unicode_normalized?(form = :nfc) ⇒ Boolean
:call-seq: str.unicode_normalized?(form=:nfc).
- #warning ⇒ Object
Methods included from Crypt::StringXor
Methods included from Crypt::Noise
Class Method Details
.natcmp(str1, str2, ignoreCase = true) ⇒ Object
from natcmp gem Natural comaration based on numbers in strings
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
# File 'lib/build/ExtendedString.rb', line 639 def self.natcmp(str1, str2, ignoreCase=true) strArrays = [str1, str2].collect do |str| str = str.downcase if ignoreCase str.tr(" \t\r\n", '').split(/(\d+)/) end minSize = strArrays.min_by { |arr| arr.size }.size 1.step(minSize-1, 2) do |i| unless strArrays.any? { |arr| arr[i] =~ /^0/ } strArrays.each { |arr| arr[i] = arr[i].to_i } end end strArrays[0] <=> strArrays[1] end |
Instance Method Details
#alarm ⇒ Object
672 673 674 |
# File 'lib/build/ExtendedString.rb', line 672 def alarm self.red end |
#bg_black ⇒ Object
684 |
# File 'lib/build/ExtendedString.rb', line 684 def bg_black; self end |
#bg_blue ⇒ Object
688 |
# File 'lib/build/ExtendedString.rb', line 688 def bg_blue; self end |
#bg_brown ⇒ Object
687 |
# File 'lib/build/ExtendedString.rb', line 687 def bg_brown; self end |
#bg_cyan ⇒ Object
690 |
# File 'lib/build/ExtendedString.rb', line 690 def bg_cyan; self end |
#bg_gray ⇒ Object
691 |
# File 'lib/build/ExtendedString.rb', line 691 def bg_gray; self end |
#bg_green ⇒ Object
686 |
# File 'lib/build/ExtendedString.rb', line 686 def bg_green; self end |
#bg_magenta ⇒ Object
689 |
# File 'lib/build/ExtendedString.rb', line 689 def bg_magenta; self end |
#bg_red ⇒ Object
685 |
# File 'lib/build/ExtendedString.rb', line 685 def bg_red; self end |
#black ⇒ Object
676 |
# File 'lib/build/ExtendedString.rb', line 676 def black; self end |
#blue ⇒ Object
680 |
# File 'lib/build/ExtendedString.rb', line 680 def blue; self end |
#bold ⇒ Object
692 |
# File 'lib/build/ExtendedString.rb', line 692 def bold; self end |
#brown ⇒ Object
679 |
# File 'lib/build/ExtendedString.rb', line 679 def brown; self end |
#camel_case(*separators) ⇒ Object
Converts a string to camelcase. This method leaves the first character as given. This allows other methods to be used first, such as #uppercase and #lowercase.
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 |
# File 'lib/build/ExtendedString.rb', line 579 def camel_case(*separators) case separators.first when Symbol, TrueClass, FalseClass, NilClass first_letter = separators.shift end separators = ['_', '\s'] if separators.empty? str = self.dup separators.each do |s| str = str.gsub(/(?:#{s}+)([a-z])/){ $1.upcase } end case first_letter when :upper, true str = str.gsub(/(\A|\s)([a-z])/){ $1 + $2.upcase } when :lower, false str = str.gsub(/(\A|\s)([A-Z])/){ $1 + $2.downcase } end str end |
#camelize(first_letter = :upper) ⇒ Object Also known as: camelcase
By default, camelize converts strings to UpperCamelCase. If the argument to camelize is set to :lower then camelize produces lowerCamelCase.
500 501 502 503 504 505 506 507 |
# File 'lib/build/ExtendedString.rb', line 500 def camelize(first_letter = :upper) case first_letter when :upper Inflections.camelize(self, true) when :lower Inflections.camelize(self, false) end end |
#capitalize_first ⇒ Object
571 572 573 574 |
# File 'lib/build/ExtendedString.rb', line 571 def capitalize_first str = to_s empty? ? str : str[0..0].upcase << str[1..-1] end |
#classify ⇒ Object
Create a class name from a plural table name like Rails does for table names to models. Note that this returns a string and not a class. (To convert to an actual class follow classify with constantize.)
549 550 551 |
# File 'lib/build/ExtendedString.rb', line 549 def classify Inflections.classify(self) end |
#constantize ⇒ Object
constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.
487 488 489 |
# File 'lib/build/ExtendedString.rb', line 487 def constantize Inflections.constantize(self) end |
#cyan ⇒ Object
682 |
# File 'lib/build/ExtendedString.rb', line 682 def cyan; self end |
#dasherize ⇒ Object
Replaces underscores with dashes in the string.
525 526 527 |
# File 'lib/build/ExtendedString.rb', line 525 def dasherize Inflections.dasherize(self) end |
#deconstantize ⇒ Object
Removes the rightmost segment from the constant expression in the string. See also demodulize.
536 537 538 |
# File 'lib/build/ExtendedString.rb', line 536 def deconstantize Inflections.deconstantize(self) end |
#demodulize ⇒ Object
Removes the module part from the constant expression in the string.
530 531 532 |
# File 'lib/build/ExtendedString.rb', line 530 def demodulize Inflections.demodulize(self) end |
#foreign_key(separate_class_name_and_id_with_underscore = true) ⇒ Object
Creates a foreign key name from a class name. separate_class_name_and_id_with_underscore sets whether the method should put ‘_’ between the name and ‘id’.
567 568 569 |
# File 'lib/build/ExtendedString.rb', line 567 def foreign_key(separate_class_name_and_id_with_underscore = true) Inflections.foreign_key(self, separate_class_name_and_id_with_underscore) end |
#gray ⇒ Object
683 |
# File 'lib/build/ExtendedString.rb', line 683 def gray; self end |
#green ⇒ Object
678 |
# File 'lib/build/ExtendedString.rb', line 678 def green; self end |
#hard_wrap(width = 80) ⇒ Object
def titleize
underscore.humanize.gsub(/\b('?[a-z])/) { $1.capitalize }
end
633 634 635 |
# File 'lib/build/ExtendedString.rb', line 633 def hard_wrap(width = 80) gsub(/(.{1,#{width}})(\s+|$)/, "\\1\n").strip end |
#humanize(options = {}) ⇒ Object
Capitalizes the first word, turns underscores into spaces, and strips a trailing ‘_id’ if present. Like titleize, this is meant for creating pretty output.
The capitalization of the first word can be turned off by setting the optional parameter capitalize to false. By default, this parameter is true.
560 561 562 |
# File 'lib/build/ExtendedString.rb', line 560 def humanize( = {}) Inflections.humanize(self, ) end |
#info ⇒ Object
664 665 666 |
# File 'lib/build/ExtendedString.rb', line 664 def info self.cyan end |
#int? ⇒ Boolean
Returns true if string contains integer value
455 456 457 |
# File 'lib/build/ExtendedString.rb', line 455 def int? Integer(self) != nil rescue false end |
#magenta ⇒ Object
681 |
# File 'lib/build/ExtendedString.rb', line 681 def magenta; self end |
#modulize ⇒ Object
Converts a string to module name representation.
This is essentially #camelcase, but it also converts ‘/’ to ‘::’ which is useful for converting paths to namespaces.
608 609 610 611 612 613 614 |
# File 'lib/build/ExtendedString.rb', line 608 def modulize #gsub('__','/'). # why was this ever here? gsub(/__(.?)/){ "::#{$1.upcase}" }. gsub(/\/(.?)/){ "::#{$1.upcase}" }. gsub(/(?:_+|-+)([a-z])/){ $1.upcase }. gsub(/(\A|\s)([a-z])/){ $1 + $2.upcase } end |
#numeric? ⇒ Boolean
Returns true if string contains numeric value
450 451 452 |
# File 'lib/build/ExtendedString.rb', line 450 def numeric? Float(self) != nil rescue false end |
#pluralize(count = nil, locale = :en) ⇒ Object
Returns the plural form of the word in the string.
If the optional parameter count is specified, the singular form will be returned if count == 1. For any other value of count the plural will be returned.
464 465 466 467 468 469 470 471 |
# File 'lib/build/ExtendedString.rb', line 464 def pluralize(count = nil, locale = :en) locale = count if count.is_a?(Symbol) if count == 1 self.dup else Inflections.pluralize(self, locale) end end |
#primary ⇒ Object
656 657 658 |
# File 'lib/build/ExtendedString.rb', line 656 def primary self.blue end |
#red ⇒ Object
677 |
# File 'lib/build/ExtendedString.rb', line 677 def red; self end |
#reverse_color ⇒ Object
694 |
# File 'lib/build/ExtendedString.rb', line 694 def reverse_color; self end |
#safe_constantize ⇒ Object
safe_constantize tries to find a declared constant with the name specified in the string. It returns nil when the name is not in CamelCase or is not initialized.
494 495 496 |
# File 'lib/build/ExtendedString.rb', line 494 def safe_constantize Inflections.safe_constantize(self) end |
#singularize(locale = :en) ⇒ Object
The reverse of pluralize, returns the singular form of a word in a string.
If the optional parameter locale is specified, the word will be singularized as a word of that language. By default, this parameter is set to :en. You must define your own inflection rules for languages other than English.
479 480 481 |
# File 'lib/build/ExtendedString.rb', line 479 def singularize(locale = :en) Inflections.singularize(self, locale) end |
#success ⇒ Object
660 661 662 |
# File 'lib/build/ExtendedString.rb', line 660 def success self.green end |
#tableize ⇒ Object
Creates the name of a table like Rails does for models to table names. This method uses the pluralize method on the last word in the string.
542 543 544 |
# File 'lib/build/ExtendedString.rb', line 542 def tableize Inflections.tableize(self) end |
#titleize ⇒ Object Also known as: titlecase
Capitalizes all the words and replaces some characters in the string to create a nicer looking title. titleize is meant for creating pretty output.
512 513 514 |
# File 'lib/build/ExtendedString.rb', line 512 def titleize Inflections.titleize(self) end |
#to_d ⇒ Object
call-seq:
string.to_d -> bigdecimal
Convert string to a BigDecimal and return it.
require 'bigdecimal'
require 'bigdecimal/util'
"0.5".to_d
# => #<BigDecimal:1dc69e0,'0.5E0',9(18)>
60 61 62 |
# File 'lib/extensions/bigdecimal/bigdecimal/util.rb', line 60 def to_d BigDecimal(self) end |
#underline ⇒ Object
693 |
# File 'lib/build/ExtendedString.rb', line 693 def underline; self end |
#underscore ⇒ Object
The reverse of camelize. Makes an underscored, lowercase form from the expression in the string.
underscore will also change ‘::’ to ‘/’ to convert namespaces to paths.
520 521 522 |
# File 'lib/build/ExtendedString.rb', line 520 def underscore Inflections.underscore(self) end |
#unicode_normalize(form = :nfc) ⇒ Object
Unicode Normalization
:call-seq:
str.unicode_normalize(form=:nfc)
Returns a normalized form of str, using Unicode normalizations NFC, NFD, NFKC, or NFKD. The normalization form used is determined by form, which is any of the four values :nfc, :nfd, :nfkc, or :nfkd. The default is :nfc.
If the string is not in a Unicode Encoding, then an Exception is raised. In this context, ‘Unicode Encoding’ means any of UTF-8, UTF-16BE/LE, and UTF-32BE/LE, as well as GB18030, UCS_2BE, and UCS_4BE. Anything else than UTF-8 is implemented by converting to UTF-8, which makes it slower than UTF-8.
Examples
"a\u0300".unicode_normalize #=> 'à' (same as "\u00E0")
"a\u0300".unicode_normalize(:nfc) #=> 'à' (same as "\u00E0")
"\u00E0".unicode_normalize(:nfd) #=> 'à' (same as "a\u0300")
"\xE0".force_encoding('ISO-8859-1').unicode_normalize(:nfd)
#=> Encoding::CompatibilityError raised
32 33 34 35 36 37 38 39 40 |
# File 'lib/framework/unicode_normalize.rb', line 32 def unicode_normalize(form = :nfc) require 'unicode_normalize/normalize.rb' unless defined? UnicodeNormalize ## The following line can be uncommented to avoid repeated checking for ## UnicodeNormalize. However, tests didn't show any noticeable speedup ## when doing this. This comment also applies to the commented out lines ## in String#unicode_normalize! and String#unicode_normalized?. # String.send(:define_method, :unicode_normalize, ->(form = :nfc) { UnicodeNormalize.normalize(self, form) } ) UnicodeNormalize.normalize(self, form) end |
#unicode_normalize!(form = :nfc) ⇒ Object
:call-seq:
str.unicode_normalize!(form=:nfc)
Destructive version of String#unicode_normalize, doing Unicode normalization in place.
48 49 50 51 52 |
# File 'lib/framework/unicode_normalize.rb', line 48 def unicode_normalize!(form = :nfc) require 'unicode_normalize/normalize.rb' unless defined? UnicodeNormalize # String.send(:define_method, :unicode_normalize!, ->(form = :nfc) { replace(unicode_normalize(form)) } ) replace(unicode_normalize(form)) end |
#unicode_normalized?(form = :nfc) ⇒ Boolean
:call-seq:
str.unicode_normalized?(form=:nfc)
Checks whether str is in Unicode normalization form form, which is any of the four values :nfc, :nfd, :nfkc, or :nfkd. The default is :nfc.
If the string is not in a Unicode Encoding, then an Exception is raised. For details, see String#unicode_normalize.
Examples
"a\u0300".unicode_normalized? #=> false
"a\u0300".unicode_normalized?(:nfd) #=> true
"\u00E0".unicode_normalized? #=> true
"\u00E0".unicode_normalized?(:nfd) #=> false
"\xE0".force_encoding('ISO-8859-1').unicode_normalized?
#=> Encoding::CompatibilityError raised
73 74 75 76 77 |
# File 'lib/framework/unicode_normalize.rb', line 73 def unicode_normalized?(form = :nfc) require 'unicode_normalize/normalize.rb' unless defined? UnicodeNormalize # String.send(:define_method, :unicode_normalized?, ->(form = :nfc) { UnicodeNormalize.normalized?(self, form) } ) UnicodeNormalize.normalized?(self, form) end |
#warning ⇒ Object
668 669 670 |
# File 'lib/build/ExtendedString.rb', line 668 def warning self.magenta end |