Project

renc

0.0
No release in over 3 years
Low commit activity in last 3 years
recursive encode for Hash and Array.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Renc

Gem Version Ruby Downloads Inline docs Code Climate Test Coverage

recursive encode for Hash and Array.

  • 1. Installation
  • 2. Usage
    • 2.1. Basic
    • 2.2. Nested Hash and Array
    • 2.3. Configuration
      • 2.3.1. Renc.default_encoding
      • 2.3.2. Renc.default_options
  • 3. Development
  • 4. Contributing
  • 5. License

1. Installation

Add this line to your application's Gemfile:

gem 'renc'

And then execute:

bundle

Or install it yourself as:

gem install renc

2. Usage

2.1. 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

2.2. 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

2.3. Configuration

2.3.1. 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>

2.3.2. 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

3. 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.


4. 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.


5. License

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


eof