Module: Random::StringExtensions
- Defined in:
- lib/garcon/core_ext/random.rb
Overview
Random extensions for String class.
Defined Under Namespace
Modules: Self
Class Method Summary collapse
Instance Method Summary collapse
-
#at_rand(separator = //) ⇒ Object
Return a random separation of the string.
-
#at_rand!(separator = //) ⇒ Object
Return a random separation while removing it from the string.
-
#rand_byte ⇒ Object
Return a random byte of self.
-
#rand_byte! ⇒ Object
Destructive rand_byte.
-
#rand_index ⇒ Object
Return a random string index.
-
#shuffle(separator = //) ⇒ Object
Return the string with seperated sections arranged in a random order.
-
#shuffle!(separator = //) ⇒ Object
In place version of shuffle.
Class Method Details
Instance Method Details
#at_rand(separator = //) ⇒ Object
Return a random separation of the string. Default separation is by charaacter.
421 422 423 |
# File 'lib/garcon/core_ext/random.rb', line 421 def at_rand(separator = //) self.split(separator, -1).at_rand end |
#at_rand!(separator = //) ⇒ Object
Return a random separation while removing it from the string. Default separation is by character.
433 434 435 436 437 438 439 440 |
# File 'lib/garcon/core_ext/random.rb', line 433 def at_rand!(separator = //) a = self.shatter(separator) w = []; a.each_with_index { |s, i| i % 2 == 0 ? w << s : w.last << s } i = SecureRandom.random_number(w.size) r = w.delete_at(i) self.replace(w.join('')) return r end |
#rand_byte ⇒ Object
Return a random byte of self.
447 448 449 |
# File 'lib/garcon/core_ext/random.rb', line 447 def rand_byte self[SecureRandom.random_number(size)] end |
#rand_byte! ⇒ Object
Destructive rand_byte. Delete a random byte of self and return it.
458 459 460 461 462 463 |
# File 'lib/garcon/core_ext/random.rb', line 458 def rand_byte! i = SecureRandom.random_number(size) rv = self[i,1] self[i,1] = '' rv end |
#rand_index ⇒ Object
Return a random string index.
470 471 472 |
# File 'lib/garcon/core_ext/random.rb', line 470 def rand_index SecureRandom.random_number(size) end |
#shuffle(separator = //) ⇒ Object
Return the string with seperated sections arranged in a random order. The default seperation is by character.
480 481 482 |
# File 'lib/garcon/core_ext/random.rb', line 480 def shuffle(separator = //) split(separator).shuffle.join('') end |
#shuffle!(separator = //) ⇒ Object
In place version of shuffle.
486 487 488 |
# File 'lib/garcon/core_ext/random.rb', line 486 def shuffle!(separator = //) self.replace(shuffle(separator)) end |