Method: Random::StringExtensions#at_rand!

Defined in:
lib/garcon/core_ext/random.rb

#at_rand!(separator = //) ⇒ Object

Return a random separation while removing it from the string. Default separation is by character.

Examples:

s = "Ruby rules"
s.at_rand!(' ')    # => "Ruby"
s                  # => "rules"


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