module Random::StringExtensions
Public Class Methods
included(base)
click to toggle source
# File lib/garcon/core_ext/random.rb, line 358 def self.included(base) base.extend(Self) end
Public Instance Methods
at_rand(separator = //)
click to toggle source
Return a random separation of the string. Default separation is by charaacter.
@example
"Ruby rules".at_rand(' ') # => ["Ruby"]
# File lib/garcon/core_ext/random.rb, line 421 def at_rand(separator = //) self.split(separator, -1).at_rand end
at_rand!(separator = //)
click to toggle source
Return a random separation while removing it from the string. Default separation is by character.
@example
s = "Ruby rules" s.at_rand!(' ') # => "Ruby" s # => "rules"
# 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()
click to toggle source
Return a random byte of self.
@example
"Ruby rules".rand_byte # => 121
# File lib/garcon/core_ext/random.rb, line 447 def rand_byte self[SecureRandom.random_number(size)] end
rand_byte!()
click to toggle source
Destructive rand_byte. Delete a random byte of self and return it.
@example
s = "Ruby rules" s.rand_byte! # => 121 s # => "Rub rules"
# 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()
click to toggle source
Return a random string index.
@example
"Ruby rules".rand_index # => 3
# File lib/garcon/core_ext/random.rb, line 470 def rand_index SecureRandom.random_number(size) end
shuffle(separator = //)
click to toggle source
Return the string with seperated sections arranged in a random order. The default seperation is by character.
@example
"Ruby rules".shuffle # => "e lybRsuur"
# File lib/garcon/core_ext/random.rb, line 480 def shuffle(separator = //) split(separator).shuffle.join('') end
shuffle!(separator = //)
click to toggle source
In place version of shuffle.
# File lib/garcon/core_ext/random.rb, line 486 def shuffle!(separator = //) self.replace(shuffle(separator)) end