Class: String
- Defined in:
- lib/amp/dependencies/zip/stdrubyext.rb,
lib/amp/support/ruby_19_compatibility.rb,
lib/amp/support/ruby_19_compatibility.rb,
lib/amp/dependencies/amp_support/ruby_amp_support.rb,
lib/amp/support/support.rb
Instance Method Summary collapse
-
#absolute(root) ⇒ Object
returns the path as an absolute path with
rootROOT MUST BE ABSOLUTE. -
#add_slashes ⇒ String
Adds minimal slashes to escape the string.
-
#any? ⇒ Boolean
String doesn’t include Enumerable in Ruby 1.9, so we lose #any?.
- #blue ⇒ Object
-
#colorize(color_code) ⇒ String
Returns the string, encoded for a tty terminal with the given color code.
- #cyan ⇒ Object
-
#end_with?(suffix) ⇒ Boolean
Does the string end with the given suffix?.
- #ends_with(aString) ⇒ Object
- #ensure_end(aString) ⇒ Object
- #green ⇒ Object
-
#hexlify ⇒ Object
Converts this text into hex.
-
#hide_password ⇒ String
removes the password from a url.
-
#is_binary_data? ⇒ Boolean
(also: #binary?)
Attempts to discern if the string represents binary data or not.
- #lchop ⇒ Object
-
#lines ⇒ Object
DON’T USE String#each.
- #magenta ⇒ Object
-
#md5 ⇒ Digest::MD5
easy md5!.
-
#not_null? ⇒ Boolean
Am I not equal to the NULL_ID used in revision logs?.
-
#null? ⇒ Boolean
Am I equal to the NULL_ID used in revision logs?.
-
#ord ⇒ Fixnum
Returns the numeric, ascii value of the first character in the string.
-
#red ⇒ Object
Returns the string, colored red.
-
#relative_path(root) ⇒ String
Returns the path from
rootto the path represented by the string. -
#remove_slashes ⇒ String
Removes minimal slashes to unescape the string.
-
#run(options = {}, args = []) ⇒ Amp::Command
If the string is the name of a command, run it.
-
#sha1 ⇒ Digest::SHA1
easy sha1! This is unsafe, as SHA1 kinda sucks.
-
#shift(char) ⇒ String
(also: #lchomp)
Pops the given character off the front of the string, but only if the string starts with the given character.
-
#short_hex ⇒ Object
Converts this text into hex, and trims it a little for readability.
-
#split_lines_better ⇒ Array<String>
Newer version of split_newlines that works better.
-
#split_newlines(add_newlines = true) ⇒ Object
Splits on newlines only, removing extra blank line at end if there is one.
-
#start_with?(prefix) ⇒ Boolean
Does the string start with the given prefix?.
- #starts_with(aString) ⇒ Object
-
#unhexlify ⇒ Object
Converts a string of hexademical into its binary representation.
- #white ⇒ Object
- #yellow ⇒ Object
Instance Method Details
#absolute(root) ⇒ Object
returns the path as an absolute path with root ROOT MUST BE ABSOLUTE
830 831 832 833 |
# File 'lib/amp/support/support.rb', line 830 def absolute(root) return self if self[0] == ?/ "#{root}/#{self}" end |
#add_slashes ⇒ String
Adds minimal slashes to escape the string
814 815 816 |
# File 'lib/amp/support/support.rb', line 814 def add_slashes self.gsub(/\\/,"\\\\").gsub(/\n/,"\\n").gsub(/\r/,"\\r").gsub("\0","\\0") end |
#any? ⇒ Boolean
String doesn’t include Enumerable in Ruby 1.9, so we lose #any?. Luckily it’s quite easy to implement.
44 45 46 |
# File 'lib/amp/support/ruby_19_compatibility.rb', line 44 def any? size > 0 end |
#blue ⇒ Object
659 |
# File 'lib/amp/support/support.rb', line 659 def blue; colorize("\e[34m"); end |
#colorize(color_code) ⇒ String
Returns the string, encoded for a tty terminal with the given color code.
651 652 653 |
# File 'lib/amp/support/support.rb', line 651 def colorize(color_code) "#{color_code}#{self}\e[0m" end |
#cyan ⇒ Object
661 |
# File 'lib/amp/support/support.rb', line 661 def cyan; colorize("\e[36m"); end |
#end_with?(suffix) ⇒ Boolean
Does the string end with the given suffix?
702 703 704 |
# File 'lib/amp/support/support.rb', line 702 def end_with?(suffix) self[-suffix.size, suffix.size] == suffix # self =~ /#{str}$/ end |
#ends_with(aString) ⇒ Object
40 41 42 |
# File 'lib/amp/dependencies/zip/stdrubyext.rb', line 40 def ends_with(aString) index(aString, -aString.size) end |
#ensure_end(aString) ⇒ Object
44 45 46 |
# File 'lib/amp/dependencies/zip/stdrubyext.rb', line 44 def ensure_end(aString) ends_with(aString) ? self : self + aString end |
#green ⇒ Object
657 |
# File 'lib/amp/support/support.rb', line 657 def green; colorize("\e[32m"); end |
#hexlify ⇒ Object
Converts this text into hex. each letter is replaced with it’s hex counterpart
782 783 784 785 786 787 788 |
# File 'lib/amp/support/support.rb', line 782 def hexlify str = "" self.each_byte do |i| str << i.to_s(16).rjust(2, "0") end str end |
#hide_password ⇒ String
removes the password from a url. else, just returns self
799 800 801 802 803 804 805 806 807 808 809 |
# File 'lib/amp/support/support.rb', line 799 def hide_password if s = self.match(/^http(?:s)?:\/\/[^:]+(?::([^:]+))?(@)/) string = '' string << self[0..s.begin(1)-1] # get from beginning to the pass string << '***' string << self[s.begin(2)..-1] string else self end end |
#is_binary_data? ⇒ Boolean Also known as: binary?
Attempts to discern if the string represents binary data or not. Not 100% accurate. Is part of the YAML code that comes with ruby, but since we don’t load rubygems, we don’t get this method for free.
842 843 844 |
# File 'lib/amp/support/support.rb', line 842 def is_binary_data? ( self.count( "^ -~", "^\r\n" ) / self.size > 0.3 || self.count( "\x00" ) > 0 ) unless empty? end |
#lchop ⇒ Object
48 49 50 |
# File 'lib/amp/dependencies/zip/stdrubyext.rb', line 48 def lchop slice(1, length) end |
#lines ⇒ Object
DON’T USE String#each. Use String#each_line
10 11 12 13 14 15 |
# File 'lib/amp/support/ruby_19_compatibility.rb', line 10 def lines return to_enum(:lines) if !block_given? self.split(/^/).each do |l| yield l end end |
#magenta ⇒ Object
660 |
# File 'lib/amp/support/support.rb', line 660 def magenta; colorize("\e[35m"); end |
#md5 ⇒ Digest::MD5
easy md5!
752 753 754 |
# File 'lib/amp/support/support.rb', line 752 def md5 Digest::MD5.new.update(self) end |
#not_null? ⇒ Boolean
Am I not equal to the NULL_ID used in revision logs?
684 685 686 |
# File 'lib/amp/support/support.rb', line 684 def not_null? !(null?) end |
#null? ⇒ Boolean
Am I equal to the NULL_ID used in revision logs?
679 680 681 |
# File 'lib/amp/support/support.rb', line 679 def null? self == Amp::RevlogSupport::Node::NULL_ID end |
#ord ⇒ Fixnum
Returns the numeric, ascii value of the first character in the string.
22 23 24 |
# File 'lib/amp/support/ruby_19_compatibility.rb', line 22 def ord self[0] end |
#red ⇒ Object
Returns the string, colored red.
656 |
# File 'lib/amp/support/support.rb', line 656 def red; colorize("\e[31m"); end |
#relative_path(root) ⇒ String
Returns the path from root to the path represented by the string. Will fail if the string is not inside root.
670 671 672 673 674 675 676 |
# File 'lib/amp/support/support.rb', line 670 def relative_path(root) return '' if self == root # return a more local path if possible... return self[root.length..-1] if start_with? root self # else we're outside the repo end |
#remove_slashes ⇒ String
Removes minimal slashes to unescape the string
821 822 823 |
# File 'lib/amp/support/support.rb', line 821 def remove_slashes self.gsub(/\\0/,"\0").gsub(/\\r/,"\r").gsub(/\\n/,"\n").gsub(/\\\\/,"\\") end |
#run(options = {}, args = []) ⇒ Amp::Command
If the string is the name of a command, run it. Else, raise hell.
772 773 774 775 776 777 778 |
# File 'lib/amp/support/support.rb', line 772 def run(={}, args=[]) if cmd = Amp::Command[self] cmd.run , args else raise "No such command #{self}" end end |
#sha1 ⇒ Digest::SHA1
easy sha1! This is unsafe, as SHA1 kinda sucks.
761 762 763 |
# File 'lib/amp/support/support.rb', line 761 def sha1 Digest::SHA1.new.update(self) end |
#shift(char) ⇒ String Also known as: lchomp
Pops the given character off the front of the string, but only if the string starts with the given character. Otherwise, nothing happens. Often used to remove troublesome leading slashes. Much like an “lchomp” method.
713 714 715 716 717 |
# File 'lib/amp/support/support.rb', line 713 def shift(char) return '' if self.empty? return self[1..-1] if self.start_with? char self end |
#short_hex ⇒ Object
Converts this text into hex, and trims it a little for readability.
792 793 794 |
# File 'lib/amp/support/support.rb', line 792 def short_hex hexlify[0..9] end |
#split_lines_better ⇒ Array<String>
Newer version of split_newlines that works better. This splits on newlines, but includes the newline in each entry in the resultant string array.
742 743 744 745 746 |
# File 'lib/amp/support/support.rb', line 742 def split_lines_better result = [] each_line {|l| result << l} result end |
#split_newlines(add_newlines = true) ⇒ Object
Splits on newlines only, removing extra blank line at end if there is one. This is how mercurial does it and i’m sticking to it. This method is evil. DON’T USE IT.
725 726 727 728 729 730 731 732 733 734 735 |
# File 'lib/amp/support/support.rb', line 725 def split_newlines(add_newlines=true) return [] if self.empty? lines = self.split("\n").map {|l| l + (add_newlines ? "\n" : "") } return lines if lines.size == 1 if (add_newlines && lines.last == "\n") || (!add_newlines && lines.last.empty?) lines.pop else lines[-1] = lines[-1][0..-2] if lines[-1][-1,1] == "\n" end lines end |
#start_with?(prefix) ⇒ Boolean
Does the string start with the given prefix?
693 694 695 |
# File 'lib/amp/support/support.rb', line 693 def start_with?(prefix) self[0,prefix.size] == prefix # self =~ /^#{str}/ end |
#starts_with(aString) ⇒ Object
36 37 38 |
# File 'lib/amp/dependencies/zip/stdrubyext.rb', line 36 def starts_with(aString) rindex(aString, 0) == 0 end |
#unhexlify ⇒ Object
Converts a string of hexademical into its binary representation. Method on strings. When the data in the string is hexademical (such as “DEADBEEF”), this method decodes the hex and converts every 2 bytes into its 1-byte binary representation.
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/amp/dependencies/amp_support/ruby_amp_support.rb', line 79 def unhexlify str = "\000" * (size/2) c = 0 (0..size-2).step(2) do |i| hex = self[i,2].to_i(16) str[c] = hex c += 1 end str end |
#white ⇒ Object
662 |
# File 'lib/amp/support/support.rb', line 662 def white; colorize("\e[37m"); end |
#yellow ⇒ Object
658 |
# File 'lib/amp/support/support.rb', line 658 def yellow; colorize("\e[33m"); end |