Class: Hash

Inherits:
Object show all
Defined in:
lib/ext_core.rb

Overview

Some extentions to the core Hash class

Constant Summary collapse

DEFAULT_GENERATIVE_KEY_TYPES =
[
    Symbol,
    String,
    Integer
]
DEFAULT_GENERATIVE_VALUE_TYPES =
[
    Integer,
    String,
    Float,
    FalseClass,
    TrueClass,
    NilClass
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate(min: 0, max: 30, key_type: nil, value_type: nil) ⇒ Object

Randomly generate a Hash



157
158
159
160
161
162
163
# File 'lib/ext_core.rb', line 157

def self.generate(min: 0, max: 30, key_type: nil, value_type: nil)
  (0..Faker::Number.between(min, max)).reduce({}) do |h, _|
    kt = key_type || DEFAULT_GENERATIVE_KEY_TYPES.sample
    vt = value_type || DEFAULT_GENERATIVE_VALUE_TYPES.sample
    h.merge(kt.generate => vt.generate)
  end
end

Instance Method Details

#generate(*_args) ⇒ Object



165
166
167
# File 'lib/ext_core.rb', line 165

def generate(*_args)
  transform_values(&:generate)
end