### Home Classes Methods

### In Files

- lib/facets/random.rb

### Methods

- #at_rand
- #at_rand!
- #pick
- #pick_key
- #pick_pair
- #rand_key
- #rand_key!
- #rand_pair
- #rand_pair!
- #rand_value
- #rand_value!
- #shuffle
- #shuffle!

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Random
- Random::ArrayExtensions
- Random::HashExtensions
- Random::IntegerExtensions
- Random::NumericExtensions
- Random::RangeExtensions
- Random::StringExtensions
- Random::StringExtensions::Self
- FileUtils
- FileUtils::DryRun
- FileUtils::NoWrite
- FileUtils::Verbose
- Digest
- Digest::Class
- Digest::Instance
- Multiton
- Multiton::Inclusive
- Multiton::MetaMethods
- URI
- URI::Hash
- URI::Kernel
- Argvector
- Argvector
- ERB
- ERB::OpenTemplate
- Enumerable
- Enumerable::Arguments
- GetoptLong
- GetoptLong::DSL
- Memoizable
- Memoizable::Copy
- Numeric
- Numeric::Multipliers
- Object
- Object::ArgVector
- Timer
- Timer::Dummy
- Array
- BasicObject
- CGI
- Cloneable
- Continuation
- Date
- DateTime
- Equitable
- File
- Find
- Hash
- Instantiable
- Integer
- Interval
- Kernel
- Math
- Module
- Net
- NilClass
- NullClass
- OpEsc
- OpenStruct
- Pathname
- Platform
- Range
- RbConfig
- Set
- Shellwords
- String
- StringScanner
- Time
- Tuple
- YAML
- Zlib

No matching classes.

# Random::HashExtensions

### Public Instance Methods

at_rand() click to toggle source
Alias for: rand_value
at_rand!() click to toggle source
Alias for: rand_value!
pick() click to toggle source
Alias for: rand_value!
pick_key() click to toggle source
Alias for: rand_key!
pick_pair() click to toggle source
Alias for: rand_pair!
rand_key() click to toggle source

Returns a random key.

    {:one => 1, :two => 2, :three => 3}.pick_key  #~> :three

    # File lib/facets/random.rb, line 243
    def rand_key
      keys.at(Random.number(keys.size))
    end

rand_key!() click to toggle source

Delete a random key-value pair, returning the key.

    a = {:one => 1, :two => 2, :three => 3}
    a.rand_key!  #~> :two
    a            #~> {:one => 1, :three => 3}

    # File lib/facets/random.rb, line 253
    def rand_key!
      k,v = rand_pair
      delete(k)
      return k
    end

Also aliased as: pick_key
rand_pair() click to toggle source

Returns a random key-value pair.

    {:one => 1, :two => 2, :three => 3}.pick  #~> [:one, 1]

    # File lib/facets/random.rb, line 265
    def rand_pair
      k = rand_key
      return k, fetch(k)
    end

rand_pair!() click to toggle source

Deletes a random key-value pair and returns that pair.

    a = {:one => 1, :two => 2, :three => 3}
    a.rand_pair!  #~> [:two, 2]
    a             #~> {:one => 1, :three => 3}

    # File lib/facets/random.rb, line 276
    def rand_pair!
      k,v = rand_pair
      delete( k )
      return k,v
    end

Also aliased as: pick_pair
rand_value() click to toggle source

Returns a random hash value.

    {:one => 1, :two => 2, :three => 3}.rand_value  #~> 2
    {:one => 1, :two => 2, :three => 3}.rand_value  #~> 1

    # File lib/facets/random.rb, line 289
    def rand_value
      fetch(rand_key)
    end

Also aliased as: at_rand
rand_value!() click to toggle source

Deletes a random key-value pair and returns the value.

    a = {:one => 1, :two => 2, :three => 3}
    a.at_rand!  #~> 2
    a           #~> {:one => 1, :three => 3}

    # File lib/facets/random.rb, line 299
    def rand_value!
      k,v = rand_pair
      delete( k )
      return v
    end

Also aliased as: pick, at_rand!
shuffle() click to toggle source

Returns a copy of the hash with values arranged in new random order.

    h = {:a=>1, :b=>2, :c=>3}
    h.shuffle  #~> {:b=>2, :c=>1, :a>3}

    # File lib/facets/random.rb, line 316
    def shuffle
      ::Hash.zip( keys.sort_by{Random.number}, values.sort_by{Random.number} )
    end

shuffle!() click to toggle source

Destructive shuffle_hash. Arrange the values in a new random order.

    h = {:a => 1, :b => 2, :c => 3}
    h.shuffle!
    h  #~> {:b=>2, :c=>1, :a=>3}

    # File lib/facets/random.rb, line 327
    def shuffle!
      self.replace(shuffle)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.