Gem Version Build Status Downloads Inline docs Code Climate Test Coverage

Renc

recursive encode for Hash and Array.

Installation

Add this line to your application's Gemfile:

gem 'renc'

And then execute:

$ bundle

Or install it yourself as:

$ gem install renc

Usage

Basic

require 'renc'

# String
str_ascii = 'hello renc!'.encode(Encoding::ASCII)
str_ascii.encoding          # => #<Encoding::US-ASCII>
str_ascii.renc.encoding     # => #<Encoding::UTF-8>
str_ascii == str_ascii.renc # => true

# Array
array_val = [str_ascii]
array_val.first.encoding      # => #<Encoding::US-ASCII>
array_val.renc.first.encoding # => #<Encoding::UTF-8>
array_val.renc == array_val   # => ture

# Hash
hash_val = { a: str_ascii }
hash_val[:a].encoding      # => #<Encoding::US-ASCII>
hash_val.renc[:a].encoding # => #<Encoding::UTF-8>
hash_val.renc == hash_val  # => true

Nested Hash and Array.

@ref ./spec/spec_helper.rb

nested = {
  a: 'a', # encoding target1
  b: {
    ba: 1,
    bb: [
      'bb0', # encoding target2
      :bb2,
      3
    ]
  }
}
nested.renc == nested # => true

# @ref ./spec/spec_helper.rb#L18
class Hash
  def values_in_nested_hash
    map { |_k, v| v.is_a?(Hash) ? v.values_in_nested_hash : v }
  end
end

encoded_string_values = nested.values_in_nested_hash # => ["a", [1, ["bb0", :bb2, 3]]]
encoded_string_values.flatten! # => ["a", 1, "bb0", :bb2, 3]
encoded_string_values.select! { |v| v.is_a?(String) } # => ["a", "bb0"]
encoded_string_values.all? { |v| v.encoding == Encoding::UTF_8 } # => true

Configuration

Renc.default_encoding

# default configure encoding is Encoding.default_external
Encoding.default_external # => #<Encoding::UTF-8>
Renc.default_encoding # => #<Encoding::UTF-8>
'test'.renc.encoding  # => #<Encoding::UTF-8>

# if you want to change to ascii
Renc.default_encoding = Encoding::ASCII
'test'.renc.encoding  # => #<Encoding::ASCII>

# change temporaly
'test'.renc(Encoding::ASCII).encoding # => #<Encoding:US-ASCII>

Renc.default_options

# default configure options is { undef: :replace }
Renc.default_options # => { undef: :replace }
'🐘'.renc  # => '?'

# if you want to change to { undef: nil }
Renc.default_options = { undef: nil }
'🐘'.renc  # => Encoding::UndefinedConversionError: U+1F418 from UTF-8 to US-ASCII

# change temporaly
'🐘'.renc(Encoding::ASCII, undef: nil).encoding # => Encoding::UndefinedConversionError: U+1F418 from UTF-8 to US-ASCII

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/k-ta-yamada/renc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.