Module: SnakyHash::Serializer::BackportedInstanceMethods

Defined in:
lib/snaky_hash/serializer.rb

Overview

Provides backported methods for older Ruby versions

Instance Method Summary collapse

Instance Method Details

#transform_values {|Object| ... } ⇒ Hash, Enumerator

Note:

This will be run in CI on Ruby 2.3, but we only collect coverage from current Ruby
Rails <= 5.2 had a transform_values method, which was added to Ruby in version 2.4.
This method is a backport of that original Rails method for Ruby 2.2 and 2.3.

:nocov:
Transforms values of a hash using the given block

Yields:

  • (Object)

    block to transform each value

Returns:

  • (Hash)

    new hash with transformed values

  • (Enumerator)

    if no block given



87
88
89
90
91
92
93
94
95
# File 'lib/snaky_hash/serializer.rb', line 87

def transform_values(&block)
  return enum_for(:transform_values) { size } unless block_given?
  return {} if empty?
  result = self.class.new
  each do |key, value|
    result[key] = yield(value)
  end
  result
end