Class: String
- Defined in:
- lib/roebe/core/string.rb,
lib/roebe/math/levensthein.rb
Overview
#
Instance Method Summary collapse
-
#>>(other) ⇒ Object
(also: #prepend)
# === >>.
-
#de_camelcase ⇒ Object
# === de_camelcase.
-
#g ⇒ Object
# === g ========================================================================= #.
-
#has_duplicate? ⇒ Boolean
# === has_duplicate? ========================================================================= #.
-
#includes_any?(*args) ⇒ Boolean
# === includes_any? ========================================================================= #.
-
#is_hex? ⇒ Boolean
# === is_hex?.
-
#is_number? ⇒ Boolean
(also: #only_numbers?)
# === is_number?.
-
#lchop ⇒ Object
# === lchop.
-
#levenshtein(compare_to_this_string, ins = 2, del = 2, sub = 1) ⇒ Object
# === levensthein ========================================================================= #.
-
#lowercase ⇒ Object
# === lowercase.
-
#mixed_case(i) ⇒ Object
# === mixed_case.
-
#my_capitalize ⇒ Object
(also: #cap, #my_cap)
# === my_capitalize.
-
#n_words? ⇒ Boolean
(also: #words?)
# === n_words?.
-
#number_with_delimiter(delimiter = '_') ⇒ Object
# === number_with_delimiter.
-
#only_caps? ⇒ Boolean
(also: #is_upcased?, #only_upcased?, #only_upcase?)
# === only_caps?.
-
#position(this_char) ⇒ Object
# === position.
-
#prefill(how_many_of = 3, this_token = '0') ⇒ Object
# === prefill.
-
#random_chars(how_many = 10, optional_upcase_randomly = false) ⇒ Object
# === random_chars.
-
#rchop ⇒ Object
# === rchop.
-
#remove_html(partial_remove = false) ⇒ Object
# === remove_html.
-
#remove_letters ⇒ Object
# === remove_letters.
-
#remove_numbers ⇒ Object
# === remove_numbers.
-
#rot13 ⇒ Object
# === rot13.
-
#split_on_word ⇒ Object
# === split_on_word.
-
#to_bool ⇒ Object
# === to_bool.
-
#to_camelcase ⇒ Object
(also: #camel_case)
# === to_camelcase.
-
#to_dir ⇒ Object
# === to_dir.
-
#to_int ⇒ Object
# === to_int.
-
#to_rgb ⇒ Object
# === to_rgb.
-
#to_version ⇒ Object
# === to_version.
-
#toggle ⇒ Object
# === toggle.
-
#uniq(token = ' ') ⇒ Object
# === uniq.
-
#unique? ⇒ Boolean
# === unique?.
-
#without_trailing(input = '/') ⇒ Object
# === without_trailing.
-
#word_count ⇒ Object
(also: #frequency)
# === word_count.
-
#word_count_for(the_data) ⇒ Object
# === word_count_for.
-
#word_wrap(line_width = 80, optional_padding = '') ⇒ Object
# === word_wrap.
-
#word_wrap!(line_width = 80, optional_padding = '') ⇒ Object
# === word_wrap!.
-
#wrap_at(my_limit = 70, add_newline = true, add_padding = nil) ⇒ Object
# === wrap_at.
Instance Method Details
#>>(other) ⇒ Object Also known as: prepend
#
>>
Prepend a string with data.
x = 'def'; x >> 'abc ' # => "abc def"
x = 'foo';x.prepend "hi_" # => "hi_foo"
#
153 154 155 156 157 |
# File 'lib/roebe/core/string.rb', line 153 def >>(other) replace(other + self) # modifies self # Another way would be with string[/^/] = "prepend_" # or self[0,0] = other end |
#de_camelcase ⇒ Object
#
de_camelcase
Another way would be this:
"WhatDoYouWant".gsub(/(\B[A-Z])/) {"_"+$1}.downcase # => "what_do_you_want"
Usage Example:
"WhatDoYouWant".de_camelcase # => "what_do_you_want"
"WhatDoYouWant".gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1 \2').split(' ')
#
624 625 626 627 |
# File 'lib/roebe/core/string.rb', line 624 def de_camelcase self.gsub(/([A-Z]+)([A-Z])/,'\1_\2'). gsub(/([a-z])([A-Z])/,'\1_\2').downcase end |
#g ⇒ Object
#
g
#
475 476 477 |
# File 'lib/roebe/core/string.rb', line 475 def g self.gsub!(/\n/,' ') end |
#has_duplicate? ⇒ Boolean
#
has_duplicate?
#
176 177 178 |
# File 'lib/roebe/core/string.rb', line 176 def has_duplicate? ! unique? end |
#includes_any?(*args) ⇒ Boolean
#
includes_any?
#
295 296 297 298 299 |
# File 'lib/roebe/core/string.rb', line 295 def includes_any?(*args) value = false args.each { |argument| value = true if self.include? argument } return value end |
#is_hex? ⇒ Boolean
#
is_hex?
Returns true if proper hex, else false.
Usage example:
"<0xfc0004>".is_hex? # => true
#
418 419 420 |
# File 'lib/roebe/core/string.rb', line 418 def is_hex? (s =~ /<0(x|X)(\d|[a-f]|[A-F])+>/) != nil end |
#is_number? ⇒ Boolean Also known as: only_numbers?
#
is_number?
Note that we ignore all - and . given to us.
"abc".is_number? # => false
"123".is_number? # => true
"a1c".is_number? # => false
"11-22".is_number? # => true
"2.22".only_numbers?
#
557 558 559 |
# File 'lib/roebe/core/string.rb', line 557 def is_number? !! (self.gsub(/\./,'').gsub(/-/,'') =~ /\A\d+\Z/) end |
#lchop ⇒ Object
#
lchop
This is like chop, but works on left side instead of right side.
#
207 208 209 |
# File 'lib/roebe/core/string.rb', line 207 def lchop slice!(0) end |
#levenshtein(compare_to_this_string, ins = 2, del = 2, sub = 1) ⇒ Object
#
levensthein
#
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/roebe/math/levensthein.rb', line 10 def levenshtein( compare_to_this_string, ins = 2, del = 2, sub = 1 ) # ins, del, sub are weighted costs return nil if self.nil? return nil if compare_to_this_string.nil? array_distance_matrix = [] # ======================================================================= # # Build up the distance matrix next: # Initialize first row values # ======================================================================= # array_distance_matrix[0] = (0 .. self.length).map { |entry| entry * ins } fill = [0] * (self.length - 1) # ======================================================================= # # Initialize first column values # ======================================================================= # for i in 1..compare_to_this_string.length array_distance_matrix[i] = [i * del, fill.flatten] end # ======================================================================= # # Populate the distance matrix next. # ======================================================================= # for i in 1..compare_to_this_string.length for j in 1..self.length # critical comparison array_distance_matrix[i][j] = [ array_distance_matrix[i-1][j-1] + (self[j-1] == compare_to_this_string[i-1] ? 0 : sub), array_distance_matrix[i][j-1] + ins, array_distance_matrix[i-1][j] + del ].min end end # ======================================================================= # # The last value in matrix is the Levenshtein distance # between the two strings. # ======================================================================= # return array_distance_matrix[compare_to_this_string.length][self.length] end |
#lowercase ⇒ Object
#
lowercase
This will lowercase the first char of a string and ONLY the first char of a string. Another way to do it would be:
str[0, 1].downcase!
Usage example:
require 'roebe/extensions'; x = "Abc"; x.lowercase; x
#
240 241 242 243 244 245 |
# File 'lib/roebe/core/string.rb', line 240 def lowercase _ = self char = _[0,1].downcase _[0,1] = char replace(_) end |
#mixed_case(i) ⇒ Object
#
mixed_case
Usage example:
mixed_case('fats waller') # => "Fats Waller"
mixed_case('louis armstrong') # => "Louis Armstrong"
mixed_case('strength in numbers') # => "Strength In Numbers"
#
45 46 47 |
# File 'lib/roebe/core/string.rb', line 45 def mixed_case(i) i.gsub(/\b\w/) {|first| first.upcase } end |
#my_capitalize ⇒ Object Also known as: cap, my_cap
#
my_capitalize
The following has annoyed me a lot about capitalize. So here is the better one, the method called my_capitalize().
It will only capitalize the first character of a string.
Usage example:
"abc_FaaBar".my_capitalize
#
435 436 437 438 439 |
# File 'lib/roebe/core/string.rb', line 435 def my_capitalize first = self[0,1].capitalize.to_s second = self[1,self.length].to_s #.downcase dont downcase here. return first+second end |
#n_words? ⇒ Boolean Also known as: words?
#
n_words?
returns how many words are in our string. “yo mama how you feeling”.n_words?
#
198 199 200 |
# File 'lib/roebe/core/string.rb', line 198 def n_words? return self.split(/\s+/).size end |
#number_with_delimiter(delimiter = '_') ⇒ Object
#
number_with_delimiter
Very cool and useful method to exchange chars in a number. number_with_delimiter(1287125897215789, ‘_’)
Original:
def number_with_delimiter(number, delimiter="_")
Usage Examples:
"12894124809".number_with_delimiter # => "12_894_124_809"
"115122894124809".number_with_delimiter # => "115_122_894_124_809"
#
577 578 579 580 581 582 |
# File 'lib/roebe/core/string.rb', line 577 def number_with_delimiter(delimiter = '_') self.gsub( /(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}" ) end |
#only_caps? ⇒ Boolean Also known as: is_upcased?, only_upcased?, only_upcase?
#
only_caps?
Returns true if ALL letters are UPPERCASED, otherwise false will be returned.
Invocation examples:
"ABC".only_caps? # => true
"AbC".only_caps? # => false
"ABC_DEF".only_caps? # => true
"cauldron_12".only_caps?
#
135 136 137 138 139 140 |
# File 'lib/roebe/core/string.rb', line 135 def only_caps? _ = self.dup value_if_all_are_caps = _.upcase.ascii_value? return true if self.ascii_value? == value_if_all_are_caps false end |
#position(this_char) ⇒ Object
#
position
Just give back a char position.
Usage Example:
require 'roebe/extensions'; "mama yo sflsf".position(' ')
#
255 256 257 |
# File 'lib/roebe/core/string.rb', line 255 def position(this_char) return self =~ /#{this_char}/ end |
#prefill(how_many_of = 3, this_token = '0') ⇒ Object
#
prefill
Usage examples:
'44'.prefill # => "00044"
'44'.prefill 10 # => "000000000044"
#
488 489 490 |
# File 'lib/roebe/core/string.rb', line 488 def prefill(how_many_of = 3, this_token = '0') return this_token * how_many_of+self end |
#random_chars(how_many = 10, optional_upcase_randomly = false) ⇒ Object
#
random_chars
Populate a string with random characters.
Usage examples:
y = ''.random_chars
y = ''.random_chars(15, true)
#
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
# File 'lib/roebe/core/string.rb', line 595 def random_chars( how_many = 10, optional_upcase_randomly = false ) alphabet = ('a' .. 'z').to_a _ = ''.dup how_many.times {|__| the_char_to_add = alphabet.sample if optional_upcase_randomly && rand(2) == 0 the_char_to_add.upcase! end _ << the_char_to_add } return _ end |
#rchop ⇒ Object
#
rchop
works like chop, but reversed. So it starts at the beginning.
Usage example:
require 'roebe/core/string.rb'; "abc".rchop # => "bc"
#
77 78 79 |
# File 'lib/roebe/core/string.rb', line 77 def rchop self.reverse.chop.reverse end |
#remove_html(partial_remove = false) ⇒ Object
#
remove_html
This method will completely remove all html tags.
if the first argument is true, we only remove < and >
"<body>".remove_html
"<body>".remove_html(true)
"<r>2</r>".remove_html
#
639 640 641 642 643 644 645 |
# File 'lib/roebe/core/string.rb', line 639 def remove_html(partial_remove = false) if partial_remove self.gsub(/\</, '').gsub(/\>/,'') else self.gsub(%r{<[^>]+>}, '') end end |
#remove_letters ⇒ Object
#
remove_letters
This method will remove all letters from the String.
"123abc".remove_letters # => "123"
Alternatively use this here:
"123abcDEF".tr('a-zA-Z', '')
#
351 352 353 |
# File 'lib/roebe/core/string.rb', line 351 def remove_letters # from a string. self.tr('a-zA-Z', '') # .gsub(/[a-z]/i,'') end |
#remove_numbers ⇒ Object
#
remove_numbers
This method will remove all numbers found within a given string.
Usage examples:
"abc 567 def".remove_numbers
"abc123".remove_numbers # => "abc"
#
392 393 394 |
# File 'lib/roebe/core/string.rb', line 392 def remove_numbers self.gsub(/\d/,'') end |
#rot13 ⇒ Object
#
rot13
Usage example:
puts 'Ruby/Python'.rot13 #-> "Ehol/Clguba"
#
500 501 502 |
# File 'lib/roebe/core/string.rb', line 500 def rot13 tr 'A-Za-z', 'N-ZA-Mn-za-m' end |
#split_on_word ⇒ Object
#
split_on_word
Invocation example:
require 'roebe/extensions'; 'CancerCell'.split_on_word # => "Cancer Cell"
#
404 405 406 |
# File 'lib/roebe/core/string.rb', line 404 def split_on_word self.gsub(/([a-z])([A-Z])/,'\1 \2') end |
#to_bool ⇒ Object
520 521 522 523 524 525 526 527 528 529 |
# File 'lib/roebe/core/string.rb', line 520 def to_bool case self when 'true' true when 'false' false else return self end end |
#to_camelcase ⇒ Object Also known as: camel_case
#
to_camelcase
This method will convert a string like ‘foo_bar’ into a string called ‘FooBar’.
Usage examples:
require 'roebe/core/string.rb'; 'abc_def'.to_camelcase # => "AbcDef"
require 'roebe/core/string.rb';'what_do_you_want'.to_camelcase # => "WhatDoYouWant"
'blah_blah_ble_foo_FaaBar'.gsub(/[^_]([a-z])/) {$1.upcase}
'blah_blah_ble_foo_FaaBar'.split('_').map { |_| _.capitalize }.join # => "BlahBlahBleFooFaaBar"
If you want to do it in one line instead:
"sample_app".split('_').map { |_| _.capitalize }.join
#
99 100 101 |
# File 'lib/roebe/core/string.rb', line 99 def to_camelcase self.split('_').map { |entry| entry.capitalize }.join end |
#to_dir ⇒ Object
#
to_dir
Simply makes a path like “foo/bar” to a proper dir path with a trailing /, thus ensures a “foo/bar/” syntax. I believe something else exists for this though.
Usage examples:
"/tmp".to_dir # => "/tmp/"
"tmp".to_dir # => "/tmp/"
#
222 223 224 225 226 227 228 |
# File 'lib/roebe/core/string.rb', line 222 def to_dir _ = self.dup.gsub(/\/\//,'/') # include sanitizing # if _ == '/' # pass through ... _ << '/' if _.size > 1 && !_.end_with?('/') _ = '/' + _ unless _.start_with? '/' return _ end |
#to_int ⇒ Object
#
to_int
“six”.to_int
#
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/roebe/core/string.rb', line 360 def to_int case self when 'one' 1 when 'two' 2 when 'three' 3 when 'four' 4 when 'five' 5 when 'six' 6 when 'seven' 7 when 'eight' 8 when 'nine' 9 end end |
#to_rgb ⇒ Object
#
to_rgb
Usage examples:
"#A2699E".to_rgb # => [162, 105, 158]
"0xFFFFFF".to_rgb # => [255, 255, 255]
#
112 113 114 115 116 117 118 119 |
# File 'lib/roebe/core/string.rb', line 112 def to_rgb if self[0,1] == '#' _ = self[1,self.length] _.hex.to_rgb else self.hex.to_rgb end end |
#to_version ⇒ Object
#
to_version
This can be used to compare i.e. Linux Kernel Versions.
Usage example:
"2.6.3".to_version > "2.6.28".to_version # => false
#
319 320 321 |
# File 'lib/roebe/core/string.rb', line 319 def to_version scan(/\d+/).map(&:to_i).extend Comparable end |
#toggle ⇒ Object
#
toggle
“true”.toggle
#
536 537 538 539 540 541 542 543 |
# File 'lib/roebe/core/string.rb', line 536 def toggle case self when 'true' return false when 'false' return true end end |
#uniq(token = ' ') ⇒ Object
#
uniq
Will return an Array with unique items.
#
306 307 308 309 |
# File 'lib/roebe/core/string.rb', line 306 def uniq(token = ' ') _ = [] self.split(token).uniq.join(token) end |
#unique? ⇒ Boolean
#
unique?
Checks whether a given String passed to us is unique or not.
"abcd".unique? # => true
"abcc".unique? # => false
#
168 169 170 171 |
# File 'lib/roebe/core/string.rb', line 168 def unique? self.each_char { |char| return false if self.count(char) > 1 } return true end |
#without_trailing(input = '/') ⇒ Object
#
without_trailing
Return string without the specified trailing char. Other characters within that string are ignored.
Usage Examples:
"/System/Executables/youtube-dl".without_trailing('/')
"ab/cd/ef/////".without_trailing('/')
"ab/cd/ef/////".without_trailing'/'.without_trailing'f'
#
286 287 288 289 290 |
# File 'lib/roebe/core/string.rb', line 286 def without_trailing(input = '/') old_string = self.dup self.count(input).times { old_string = old_string.chomp(input) } return old_string end |
#word_count ⇒ Object Also known as: frequency
#
word_count
This little method counts word frequencies.
It will return a hash with these frequencies.
Usage examples for it:
require 'roebe/extensions'; x = %{ Dogs cat? cat? cat dog dog dog cat dog eagle dog dogs }.word_count
=> { "cat"=>2, "dogs"=>2, "eagle"=>1, "dog"=>5, "cat?"=>2 }
#
336 337 338 339 340 |
# File 'lib/roebe/core/string.rb', line 336 def word_count hash = Hash.new(0) self.scan(/\w+\??/) { |word| hash[word] += 1 } return hash end |
#word_count_for(the_data) ⇒ Object
#
word_count_for
Counts how many specific words are in a given string:
"lalal desc descccc lalala".word_count_for('desc') # => 2
#
188 189 190 |
# File 'lib/roebe/core/string.rb', line 188 def word_count_for(the_data) self.scan(/#{the_data}/).size.to_i end |
#word_wrap(line_width = 80, optional_padding = '') ⇒ Object
#
word_wrap
long_string.word_wrap(75)
#
25 26 27 28 29 30 31 32 33 |
# File 'lib/roebe/core/string.rb', line 25 def word_wrap( line_width = 80, optional_padding = '' ) new_text = self.dup.gsub( /(.{1,#{line_width}})(\s+|$)/, optional_padding+"\\1\n" ) return new_text end |
#word_wrap!(line_width = 80, optional_padding = '') ⇒ Object
#
word_wrap!
This is a beautification method for splitting lines. (String beautifier)
The argument “optional_padding” will pad to the left.
Usage example:
x = ( ("Paragraph one two three " * 80) + ( "a" * ( 1+rand(333) ) )+' ' ); puts x.word_wrap!(80, ' ')
#
61 62 63 64 65 |
# File 'lib/roebe/core/string.rb', line 61 def word_wrap!(line_width = 80, optional_padding = '') replace( self.dup.word_wrap(line_width, optional_padding) ) # modifies self end |
#wrap_at(my_limit = 70, add_newline = true, add_padding = nil) ⇒ Object
#
wrap_at
Wraps at a specified position.
Usage example:
puts "abc abc abdc abc abc abc abc abc abc abc abcdef abc abc abc abc abc abc abc abc abc ".wrap_at(20)
puts ("x"*200).wrap_at(30)
("x"*200).wrap_at(30,true,5)
#
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 |
# File 'lib/roebe/core/string.rb', line 454 def wrap_at(my_limit = 70, add_newline = true, add_padding = nil) add_padding = add_padding.to_i unless add_padding.nil? if add_newline _ = "\\1\\3\n" # newline at end unless add_padding.nil? _ = _.linsert(' ' * add_padding) end return self.gsub(/(.{1,#{my_limit}})( +|$)\n?|(.{#{my_limit}})/,_) else _ = "\\1\\3" unless add_padding.nil? _ = _.linsert(' '*add_padding) end return self.gsub(/(.{1,#{my_limit}})( +|$)\n?|(.{#{my_limit}})/, _) # no newline at end end end |