Class: String
Direct Known Subclasses
IOStub
Class Method Summary
collapse
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, camelize
converts strings to UpperCamelCase.
-
#capitalize_first ⇒ Object
-
#center(width, str = " ") ⇒ Object
-
#classify ⇒ Object
Create a class name from a plural table name like Rails does for table names to models.
-
#constantize ⇒ Object
constantize
tries 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.
-
#empty? ⇒ Boolean
-
#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
-
#humanize(options = {}) ⇒ Object
Capitalizes the first word, turns underscores into spaces, and strips a trailing ‘_id’ if present.
-
#index(x, *start) ⇒ Object
-
#info ⇒ Object
-
#inspect ⇒ Object
-
#int? ⇒ Boolean
Returns true if string contains integer value.
-
#justify_string(width, str, justify) ⇒ Object
justify left = -1, center = 0, right = 1.
-
#ljust(width, str = " ") ⇒ Object
-
#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
-
#rjust(width, str = " ") ⇒ Object
-
#safe_constantize ⇒ Object
safe_constantize
tries 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.
-
#slice!(*args) ⇒ Object
-
#success ⇒ Object
-
#sum(n = 16) ⇒ 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_s ⇒ Object
(also: #to_str)
-
#underline ⇒ Object
-
#underscore ⇒ Object
-
#warning ⇒ Object
#^
#add_noise, #remove_noise
Methods included from Comparable
#<, #<=, #==, #>, #>=, #between?
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
|
#center(width, str = " ") ⇒ Object
803
804
805
|
# File 'lib/framework/builtinME.rb', line 803
def center(width, str=" ")
justify_string(width, str, 0)
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.
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
.
#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
|
#empty? ⇒ Boolean
767
768
769
|
# File 'lib/framework/builtinME.rb', line 767
def empty?
length == 0
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
|
#index(x, *start) ⇒ Object
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
|
# File 'lib/framework/builtinME.rb', line 740
def index(x, *start)
begin_index = 0
if start.size > 0
begin_index = start[0]
begin_index = self.length + begin_index if begin_index < 0
end
if String === x
begin_index.upto(self.length) do |i|
if self[i, x.length] == x
return i
end
end
elsif Fixnum === x
begin_index.upto(self.length) do |i|
if self[i] == x
return i
end
end
else x = (x =~ (self[begin_index, self.length-begin_index]))
if x
return x + begin_index
end
return nil
end
end
|
664
665
666
|
# File 'lib/build/ExtendedString.rb', line 664
def info
self.cyan
end
|
771
772
773
774
|
# File 'lib/framework/builtinME.rb', line 771
def inspect
s = gsub("\\", "\\\\")
'"' + s.gsub("\"", "\\\"") + '"'
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
|
#justify_string(width, str, justify) ⇒ Object
justify left = -1, center = 0, right = 1
778
779
780
781
782
783
784
785
786
787
788
789
790
|
# File 'lib/framework/builtinME.rb', line 778
def justify_string(width, str, justify)
return self if width <= length
pad = width - length
out = str.to_str * (pad / str.length)
out << str[0, pad - out.length] if out.length < pad
return self << out if justify == -1
return out << self if justify == 1
split = (width / 2) - (length / 2)
return out.insert(split-((width-length)%2), self)
end
|
#ljust(width, str = " ") ⇒ Object
798
799
800
|
# File 'lib/framework/builtinME.rb', line 798
def ljust(width, str=" ")
justify_string(width, str, -1)
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
|
#rjust(width, str = " ") ⇒ Object
793
794
795
|
# File 'lib/framework/builtinME.rb', line 793
def rjust(width, str=" ")
justify_string(width, str, 1)
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.
#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
|
#slice!(*args) ⇒ Object
810
811
812
813
814
815
816
817
818
819
820
821
822
823
|
# File 'lib/framework/builtinME.rb', line 810
def slice!(*args)
res = ""
if args.size() == 1
res = slice(args[0])
self[args[0]]= "" if res
elsif args.size() == 2
res = slice(args[0], args[1])
self[args[0], args[1]]= "" if res
else
raise ArgumentError, 'String.slice! - wrong number of arguments 3 for (1 or 2)'
end
res
end
|
660
661
662
|
# File 'lib/build/ExtendedString.rb', line 660
def success
self.green
end
|
#sum(n = 16) ⇒ Object
730
731
732
733
734
735
736
737
738
|
# File 'lib/framework/builtinME.rb', line 730
def sum(n=16)
sum = 0
each_byte {|x| sum += x}
if n != 0
return sum & ((1 << n) - 1)
else
return sum
end
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
|
#to_s ⇒ Object
Also known as:
to_str
726
727
728
|
# File 'lib/framework/builtinME.rb', line 726
def to_s
return 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
|