Class: String
Direct Known Subclasses
IOStub
Class Method Summary
collapse
Instance Method Summary
collapse
#^
#add_noise, #remove_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
672
673
674
|
# File 'lib/build/ExtendedString.rb', line 672
def alarm
self.red
end
|
684
|
# File 'lib/build/ExtendedString.rb', line 684
def bg_black; self end
|
688
|
# File 'lib/build/ExtendedString.rb', line 688
def bg_blue; self end
|
687
|
# File 'lib/build/ExtendedString.rb', line 687
def bg_brown; self end
|
690
|
# File 'lib/build/ExtendedString.rb', line 690
def bg_cyan; self end
|
691
|
# File 'lib/build/ExtendedString.rb', line 691
def bg_gray; self end
|
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
|
685
|
# File 'lib/build/ExtendedString.rb', line 685
def bg_red; self end
|
676
|
# File 'lib/build/ExtendedString.rb', line 676
def black; self end
|
680
|
# File 'lib/build/ExtendedString.rb', line 680
def blue; self end
|
692
|
# File 'lib/build/ExtendedString.rb', line 692
def bold; self end
|
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
|
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
|
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
|
683
|
# File 'lib/build/ExtendedString.rb', line 683
def gray; self end
|
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(options = {})
Inflections.humanize(self, options)
end
|
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
|
681
|
# File 'lib/build/ExtendedString.rb', line 681
def magenta; self end
|
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(/__(.?)/){ "::#{$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
|
656
657
658
|
# File 'lib/build/ExtendedString.rb', line 656
def primary
self.blue
end
|
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
|
660
661
662
|
# File 'lib/build/ExtendedString.rb', line 660
def success
self.green
end
|
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
|
#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
|
668
669
670
|
# File 'lib/build/ExtendedString.rb', line 668
def warning
self.magenta
end
|