Class: String

Inherits:
Object
  • Object
show all
Includes:
Zombify::String::ZombifyHelper
Defined in:
lib/rand.rb,
lib/zombify.rb

Instance Method Summary collapse

Methods included from Zombify::String::ZombifyHelper

#zombify

Instance Method Details

#pick_byteObject

Return a random byte of self.

"Ruby rules".pick_byte  #=> 121


185
186
187
# File 'lib/rand.rb', line 185

def pick_byte
  self[pick_index]
end

#pick_byte!Object

Destructive pick_byte. Delete a random byte of self and return it.

s = "Ruby rules"
s.pick_byte!  #=> 121
s             #=> "Rub rules"


211
212
213
# File 'lib/rand.rb', line 211

def pick_byte!
  pick_char![0]
end

#pick_charObject

Return a single-character string of a random character in self.

"Ruby rules".pick_char  #=> "y"


191
192
193
# File 'lib/rand.rb', line 191

def pick_char
  pick_byte.chr
end

#pick_char!Object

Destructive pick_char. Delete a random character of the string and return it as a single-character string.

s = "Ruby rules"
s.pick_char!  #=> "y"
s             #=> "Rub rules"


200
201
202
203
204
205
# File 'lib/rand.rb', line 200

def pick_char!
  i = pick_index
  rv = self[i,1]
  self[i,1] = ""
  rv
end

#pick_indexObject

Return a random byte index of self.

"Ruby rules".pick_index  #=> 3


217
218
219
# File 'lib/rand.rb', line 217

def pick_index
  rand(size)
end

#pick_index!Object

Destructive pick_index. Delete a random byte of self and return it’s index.

s = "Ruby rules"
s.pick_index  #=> 3
s             #=> "Rub rules"


226
227
228
229
230
# File 'lib/rand.rb', line 226

def pick_index!
  i = pick_index
  self[i,1] = ""
  i
end

#shuffle_charsObject

Return the string with characters arranged in random order.

"Ruby rules".shuffle_chars  #=> "e lybRsuur"


170
171
172
# File 'lib/rand.rb', line 170

def shuffle_chars
  split(//).shuffle.join('')
end

#shuffle_chars!Object

Destructive shuffle_chars. Arrange the characters of the string in new, random order.

s = "Ruby rules".shuffle_chars
s.shuffle_chars!
s    #=> "e lybRsuur"


179
180
181
# File 'lib/rand.rb', line 179

def shuffle_chars!
  self[0,size] = shuffle_chars
end