Module | Random::String |
In: |
lib/more/facets/random.rb
|
# File lib/more/facets/random.rb, line 318 318: def self.included(base) 319: base.extend(Self) 320: end
Return a random separation of the string. Default separation is by charaacter.
"Ruby rules".at_rand(' ') #=> ["Ruby"]
# File lib/more/facets/random.rb, line 366 366: def at_rand( separator=// ) 367: #separator = self.class.patterns( separator ) 368: self.split(separator,-1).at_rand 369: end
Return a random separation while removing it from the string. Default separation is by character.
s = "Ruby rules" s = at_rand!(' ') #=> "Ruby" s #=> "rules"
# File lib/more/facets/random.rb, line 378 378: def at_rand!( separator=// ) 379: #separator = self.class.patterns( separator ) 380: a = self.shatter( separator ) 381: w = []; a.each_with_index { |s,i| i % 2 == 0 ? w << s : w.last << s } 382: i = Random.number(w.size) 383: r = w.delete_at( i ) 384: self.replace( w.join('') ) 385: return r 386: end
Return a random byte of self.
"Ruby rules".rand_byte #=> 121
# File lib/more/facets/random.rb, line 392 392: def rand_byte 393: self[Random.number(size)] 394: end
Return a random string index.
"Ruby rules".rand_index #=> 3
# File lib/more/facets/random.rb, line 413 413: def rand_index 414: Random.number(size) 415: end
Return the string with seperated sections arranged in a random order. The default seperation is by character.
"Ruby rules".shuffle #=> "e lybRsuur"
# File lib/more/facets/random.rb, line 422 422: def shuffle(separator=//) 423: split(separator).shuffle.join('') 424: end