Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/rand.rb

Instance Method Summary collapse

Instance Method Details

#pickObject



300
301
302
# File 'lib/rand.rb', line 300

def pick
  to_a.pick
end

#pick!Object



304
305
306
# File 'lib/rand.rb', line 304

def pick!
  pick_with_index![0]
end

#pick_byteObject

Return a random byte of self.

"Ruby rules".pick_byte  #=> 121


215
216
217
# File 'lib/rand.rb', line 215

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"


241
242
243
# File 'lib/rand.rb', line 241

def pick_byte!
  pick_char![0]
end

#pick_byte_with_indexObject

Return a two element array consisting of an random byte of self and it’s index.

"Ruby rules".pick_byte_with_index  #=> [121, 3]


265
266
267
268
# File 'lib/rand.rb', line 265

def pick_byte_with_index
  i = pick_index
  [self[i], i]
end

#pick_byte_with_index!Object

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

s = "Ruby rules"
s.pick_byte_with index!  #=> [121, 3]
s                        #=> "Rub rules"


275
276
277
278
279
# File 'lib/rand.rb', line 275

def pick_byte_with_index!
  rv = pick_byte_with_index
  delete_at(rv[0])
  rv
end

#pick_charObject

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

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


221
222
223
# File 'lib/rand.rb', line 221

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"


230
231
232
233
234
235
# File 'lib/rand.rb', line 230

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

#pick_char_with_indexObject

Return a single-character string of a random character in self and it’s index.

"Ruby rules".pick_char_with_index  #=> ["y", 3]


284
285
286
287
# File 'lib/rand.rb', line 284

def pick_char_with_index
  byte, index = pick_byte_with_index
  [byte.chr, index]
end

#pick_char_with_index!Object

Destructive pick_char_with_index. Delete a random character of the string and return it as a single-character string together with it’s index.

s = "Ruby rules"
s.pick_char_with_index!  #=> ["y", 3]
s                        #=> "Rub rules"


295
296
297
298
# File 'lib/rand.rb', line 295

def pick_char_with_index!
  byte, index = pick_byte_with_index!
  [byte.chr, index]
end

#pick_indexObject

Return a random byte index of self.

"Ruby rules".pick_index  #=> 3


247
248
249
# File 'lib/rand.rb', line 247

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"


256
257
258
259
260
# File 'lib/rand.rb', line 256

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

#pick_with_indexObject



308
309
310
# File 'lib/rand.rb', line 308

def pick_with_index
  to_a.pick_with_index
end

#pick_with_index!Object



312
313
314
315
316
317
318
# File 'lib/rand.rb', line 312

def pick_with_index!
  tokens = to_a
  s, index = tokens.pick_with_index
  start = tokens[0, index].join
  self[start.size, s.size] = ""
  [s, index]
end

#shuffle_charsObject

Return the string with characters arranged in random order.

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


196
197
198
# File 'lib/rand.rb', line 196

def shuffle_chars
  dup.shuffle_chars!
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"


205
206
207
208
209
210
211
# File 'lib/rand.rb', line 205

def shuffle_chars!
  (0...size).each {|j| 
    i = rand(size-j)
    self[j], self[j+i] = self[j+i], self[j]
  }
  self
end