Module: Renc

Extended by:
Configuration
Defined in:
lib/renc.rb,
lib/renc/version.rb,
lib/renc/configuration.rb

Overview

recursive encode for Hash and Array.

Examples:

require 'renc'

# or context `main`
extend Renc

See Also:

Defined Under Namespace

Modules: Configuration

Constant Summary collapse

TARGET_CLASS =

for include #renc method

[String, Array, Hash, Struct].freeze
VERSION =
'2.2.2'.freeze
ERR_MESSAGE_ENCODING =
'argument `encoding` is not a Encoding Class'.freeze
ERR_MESSAGE_OPTIONS =
'argument `options` is not a Hash Class'.freeze

Constants included from Configuration

Configuration::DEFAULT_ENCODING, Configuration::DEFAULT_OPTIONS

Instance Method Summary collapse

Methods included from Configuration

default_encoding, default_encoding=, default_options, default_options=

Instance Method Details

#renc(encoding = Renc.default_encoding, options = Renc.default_options) ⇒ Object

recursive encode for Hash and Array.

Examples:

# for example
default_src_encoding # => #<Encoding:UTF-8>

# Hash values
result = { a: 'a', b: 'b', c: 'c' }.renc(Encoding::ASCII)
result # => { a: 'a', b: 'b', c: 'c' }
result.values.map(&:encoding).all? { Encoding::ASCII } # => true

# Array values
result = %w(a b c).renc(Encoding::ASCII)
result # => ['a', 'b', 'c']
result.map(&:encoding).all? { Encoding::ASCII } # => true

# with configure default_encoding
Renc.default_encoding = Encoding::ASCII
result = 'hello'.renc
result # => 'hello'
result.encoding # => Encoding::ASCII

Parameters:

  • encoding (Encoding) (defaults to: Renc.default_encoding)
  • options (Hash) (defaults to: Renc.default_options)

Returns:

  • (Object)

Raises:

  • (TypeError)

See Also:

  • default_encoding
  • default_options


43
44
45
46
47
48
49
50
51
# File 'lib/renc.rb', line 43

def renc(encoding = Renc.default_encoding, options = Renc.default_options)
  raise TypeError, ERR_MESSAGE_ENCODING unless encoding.is_a?(Encoding)
  raise TypeError, ERR_MESSAGE_OPTIONS unless options.is_a?(Hash)

  @encoding = encoding
  @options  = options

  _renc(self)
end