Class: String

Inherits:
Object show all
Includes:
Comparable, Crypt::Noise, Crypt::StringXor
Defined in:
lib/framework/builtinME.rb,
lib/build/ExtendedString.rb,
lib/extensions/crypt/crypt/noise.rb,
lib/extensions/crypt/crypt/stringxor.rb

Direct Known Subclasses

IOStub

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Crypt::StringXor

#^

Methods included from Crypt::Noise

#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

#alarmObject



672
673
674
# File 'lib/build/ExtendedString.rb', line 672

def alarm
  self.red
end

#bg_blackObject



684
# File 'lib/build/ExtendedString.rb', line 684

def bg_black; self end

#bg_blueObject



688
# File 'lib/build/ExtendedString.rb', line 688

def bg_blue; self end

#bg_brownObject



687
# File 'lib/build/ExtendedString.rb', line 687

def bg_brown; self end

#bg_cyanObject



690
# File 'lib/build/ExtendedString.rb', line 690

def bg_cyan; self end

#bg_grayObject



691
# File 'lib/build/ExtendedString.rb', line 691

def bg_gray; self end

#bg_greenObject



686
# File 'lib/build/ExtendedString.rb', line 686

def bg_green; self end

#bg_magentaObject



689
# File 'lib/build/ExtendedString.rb', line 689

def bg_magenta; self end

#bg_redObject



685
# File 'lib/build/ExtendedString.rb', line 685

def bg_red; self end

#blackObject



676
# File 'lib/build/ExtendedString.rb', line 676

def black; self end

#blueObject



680
# File 'lib/build/ExtendedString.rb', line 680

def blue; self end

#boldObject



692
# File 'lib/build/ExtendedString.rb', line 692

def bold; self end

#brownObject



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_firstObject



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

from rubinius



803
804
805
# File 'lib/framework/builtinME.rb', line 803

def center(width, str=" ")
    justify_string(width, str, 0)
end

#classifyObject

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

#constantizeObject

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

#cyanObject



682
# File 'lib/build/ExtendedString.rb', line 682

def cyan; self end

#dasherizeObject

Replaces underscores with dashes in the string.



525
526
527
# File 'lib/build/ExtendedString.rb', line 525

def dasherize
  Inflections.dasherize(self)
end

#deconstantizeObject

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

#demodulizeObject

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

Returns:

  • (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

#grayObject



683
# File 'lib/build/ExtendedString.rb', line 683

def gray; self end

#greenObject



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 #regex
        x = (x =~ (self[begin_index, self.length-begin_index]))
        if x
            return x + begin_index
        end
        return nil
    end
end

#infoObject



664
665
666
# File 'lib/build/ExtendedString.rb', line 664

def info
  self.cyan
end

#inspectObject



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

Returns:

  • (Boolean)


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
    # Left justification
    return self << out if justify == -1
    # Right justification
    return out << self if justify == 1
    # and finially center
    split = (width / 2) - (length / 2)
    return out.insert(split-((width-length)%2), self)
end

#ljust(width, str = " ") ⇒ Object

from rubinius



798
799
800
# File 'lib/framework/builtinME.rb', line 798

def ljust(width, str=" ")
    justify_string(width, str, -1)
end

#magentaObject



681
# File 'lib/build/ExtendedString.rb', line 681

def magenta; self end

#modulizeObject

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

Returns:

  • (Boolean)


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

#primaryObject



656
657
658
# File 'lib/build/ExtendedString.rb', line 656

def primary
  self.blue
end

#redObject



677
# File 'lib/build/ExtendedString.rb', line 677

def red; self end

#reverse_colorObject



694
# File 'lib/build/ExtendedString.rb', line 694

def reverse_color; self end

#rjust(width, str = " ") ⇒ Object

from rubinius



793
794
795
# File 'lib/framework/builtinME.rb', line 793

def rjust(width, str=" ")
    justify_string(width, str, 1)
end

#safe_constantizeObject

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

#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

#successObject



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

#tableizeObject

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

#titleizeObject 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_sObject Also known as: to_str



726
727
728
# File 'lib/framework/builtinME.rb', line 726

def to_s
    return self
end

#underlineObject



693
# File 'lib/build/ExtendedString.rb', line 693

def underline; self end

#underscoreObject

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

#warningObject



668
669
670
# File 'lib/build/ExtendedString.rb', line 668

def warning
  self.magenta
end