Class: String
Overview
Overloads the String class.
Instance Method Summary collapse
- #binary? ⇒ Boolean
-
#diff_ratio(other) ⇒ Float
Calculates the difference ratio (at a word level) between ‘self` and `other`.
-
#longest_word ⇒ String
Longest word.
-
#persistent_hash ⇒ Integer
In integer with the property of:.
-
#rdiff(other) ⇒ String
Gets the reverse diff between self and str on a word level.
- #recode ⇒ Object
- #recode! ⇒ Object
- #repack ⇒ Object
-
#shortest_word ⇒ String
Shortest word.
- #substring?(string) ⇒ Boolean
-
#words(strict = false) ⇒ Array<String>
Returns the words in ‘self`.
Instance Method Details
#binary? ⇒ Boolean
138 139 140 141 142 143 |
# File 'lib/arachni/ruby/string.rb', line 138 def binary? # Stolen from YAML. encoding == Encoding::ASCII_8BIT || index("\x00") || count("\x00-\x7F", "^ -~\t\r\n").fdiv(length) > 0.3 end |
#diff_ratio(other) ⇒ Float
Calculates the difference ratio (at a word level) between ‘self` and `other`
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/arachni/ruby/string.rb', line 66 def diff_ratio( other ) return 0.0 if self == other return 1.0 if empty? || other.empty? s_words = self.words( true ) o_words = other.words( true ) common = (s_words & o_words).size.to_f union = (s_words | o_words).size.to_f (union - common) / union end |
#longest_word ⇒ String
Returns Longest word.
98 99 100 |
# File 'lib/arachni/ruby/string.rb', line 98 def longest_word words( true ).sort_by { |w| w.size }.last end |
#persistent_hash ⇒ Integer
Returns In integer with the property of:
If ‘str1 == str2` then `str1.persistent_hash == str2.persistent_hash`.
It basically has the same function as Ruby’s ‘#hash` method, but does not use a random seed per Ruby process – making it suitable for use in distributed systems.
111 112 113 |
# File 'lib/arachni/ruby/string.rb', line 111 def persistent_hash Zlib.crc32 self end |
#rdiff(other) ⇒ String
Gets the reverse diff between self and str on a word level.
str = <<END
This is the first test.
Not really sure what else to put here...
END
str2 = <<END
This is the second test.
Not really sure what else to put here...
Boo-Yah!
END
str.rdiff( str2 )
# => "This is the test.\nNot really sure what else to put here...\n"
49 50 51 52 53 54 55 56 57 |
# File 'lib/arachni/ruby/string.rb', line 49 def rdiff( other ) return self if self == other # get the words of the first text in an array s_words = words # get what hasn't changed (the rdiff, so to speak) as a string (s_words - (s_words - other.words)).join end |
#recode ⇒ Object
133 134 135 |
# File 'lib/arachni/ruby/string.rb', line 133 def recode dup.recode! end |
#recode! ⇒ Object
128 129 130 131 |
# File 'lib/arachni/ruby/string.rb', line 128 def recode! force_encoding( 'utf-8' ) encode!( 'utf-16be', invalid: :replace, undef: :replace ).encode( 'utf-8' ) end |
#repack ⇒ Object
124 125 126 |
# File 'lib/arachni/ruby/string.rb', line 124 def repack unpack( 'C*' ).pack( 'U*' ) end |
#shortest_word ⇒ String
Returns Shortest word.
93 94 95 |
# File 'lib/arachni/ruby/string.rb', line 93 def shortest_word words( true ).sort_by { |w| w.size }.first end |
#substring?(string) ⇒ Boolean
115 116 117 118 119 120 121 122 |
# File 'lib/arachni/ruby/string.rb', line 115 def substring?( string ) begin cmatch = match( Regexp.new( Regexp.escape( string ) ) ) cmatch && !cmatch.to_s.empty? rescue nil end end |