Sha256: 7ce7569ead50c472603a788174ea24c4121475dc5f85959fe8c2dc5492931f9e

Contents?: true

Size: 820 Bytes

Versions: 1

Compression:

Stored size: 820 Bytes

Contents

require 'renc/version'

# namespace
module Renc
  # recurse encoding for Hash and Array.
  # @param obj [Object]
  # @param encoding [Encoding]
  # @return [Object]
  def renc(obj, encoding = Encoding::UTF_8)
    case obj
    when String then obj.encode(encoding)
    when Hash   then enc_hash(obj, encoding)
    when Array  then enc_array(obj, encoding)
    else             obj
    end
  end

  # @see #renc
  alias enc renc
  extend Gem::Deprecate
  deprecate :enc, :renc, 2016, 3

  private

  # recurse encoding for Hash values of String.
  def enc_hash(obj, encoding)
    obj.each_with_object({}) do |args, h|
      key, val = args
      h[key] = renc(val, encoding)
    end
  end

  # recurse encoding for Array values of String.
  def enc_array(obj, encoding)
    obj.map { |val| renc(val, encoding) }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
renc-1.0.1 lib/renc.rb