Module: Darthjee::CoreExt::Hash::Changeable

Included in:
Darthjee::CoreExt::Hash
Defined in:
lib/darthjee/core_ext/hash/changeable.rb

Overview

Methods responsible for changing hash values

Author:

  • Darthjee

Instance Method Summary collapse

Instance Method Details

#change_values(options = {}) {|value| ... } ⇒ ::Hash

Creates a new hash with changes in its values

Examples:

Simple Usage

hash = { a: 1, b: 2 }
hash.change_values do |value|
  value + 1
end                     # returns { a: 2, b: 3 }

Skipping inner hash transformation

hash = { a: 1, b: { c: 1 } }

hash.change_values(&:to_s)) # returns {
                            #   a: "1",
                            #   b: { c: "1" }
                            # }

Not skipping inner hash transformation

hash = { a: 1, b: { c: 1 } }

hash.change_values(skip_inner: false, &:to_s))
                            # returns {
                            #   a: "1",
                            #   b: "{:c=>1}"
                            # }

Parameters:

  • options (::Hash) (defaults to: {})

Options Hash (options):

  • recursive (::TrueClass, ::FalseClass) — default: true

    flag indicating recursive sorting

  • skip_inner (::TrueClass, ::FalseClass) — default: true

    Flag indicating to skip running transformation on Hash objects

Yields:

  • (value)

    changing value block

Returns:



47
48
49
# File 'lib/darthjee/core_ext/hash/changeable.rb', line 47

def change_values(options = {}, &block)
  deep_dup.change_values!(options, &block)
end

#change_values!(options = {}) {|value| ... } ⇒ ::Hash

Changes the values of a hash

Examples:

Changing inner hash

inner_hash = { c: 2 }
hash = { a: 1, b: inner_hash }

hash.change_values!(&:to_s)

inner_hash # changed to { c: "2" }

Not changing inner hash

inner_hash = { c: 2 }
hash = { a: 1, b: inner_hash }

hash.change_values!(skip_inner: false, &:to_s)

hash       # changed to { a: "1", b: "{:c=>2}" }
inner_hash # still      { c: 2 }

Simple Usage

hash = { a: 1, b: 2 }
hash.change_values do |value|
  value + 1
end                     # returns { a: 2, b: 3 }

Skipping inner hash transformation

hash = { a: 1, b: { c: 1 } }

hash.change_values(&:to_s)) # returns {
                            #   a: "1",
                            #   b: { c: "1" }
                            # }

Not skipping inner hash transformation

hash = { a: 1, b: { c: 1 } }

hash.change_values(skip_inner: false, &:to_s))
                            # returns {
                            #   a: "1",
                            #   b: "{:c=>1}"
                            # }

Parameters:

  • options (::Hash) (defaults to: {})

Options Hash (options):

  • recursive (::TrueClass, ::FalseClass) — default: true

    flag indicating recursive sorting

  • skip_inner (::TrueClass, ::FalseClass) — default: true

    Flag indicating to skip running transformation on Hash objects

Yields:

  • (value)

    changing value block

Returns:



82
83
84
# File 'lib/darthjee/core_ext/hash/changeable.rb', line 82

def change_values!(options = {}, &block)
  Hash::ValueChanger.new(options, &block).change(self)
end