Sha256: d377b5b6b91f812a9ef2e17b3a122847f4990f99333e5b8ffeebdf15a3570e18

Contents?: true

Size: 1.05 KB

Versions: 6

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

String.class_eval do
  unless method_defined?(:clean!)
    # Remove all non-UTF-8 chars from a string.
    #
    # @param replace_with [String, nil] What to replace invalid chars with.
    #
    # @return [String]
    #
    def clean!(replace_with = '')
      replace(encode('UTF-8', invalid: :replace, undef: :replace, replace: replace_with))
    end
  end

  unless method_defined?(:clean)
    # @see {#clean!}
    def clean(replace_with = '')
      dup.clean!(replace_with)
    end
  end

  unless method_defined?(:clean_binary!)
    # Remove all non-UTF-8 chars from a string and convert to binary.
    #
    # @param replace_with [String, nil] What to replace invalid chars with.
    #
    # @return [String]
    #
    def clean_binary!(replace_with = '')
      replace(encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: replace_with))
    end
  end

  unless method_defined?(:clean_binary)
    # @see {#clean_binary!}
    def clean_binary(replace_with = '')
      dup.clean_binary!(replace_with)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ruby-rails-extensions-1.1.1 lib/ruby-rails-extensions/extensions/clean_string.rb
ruby-rails-extensions-1.1.1.pre.rc.1 lib/ruby-rails-extensions/extensions/clean_string.rb
ruby-rails-extensions-0.2.0 lib/ruby-rails-extensions/extensions/clean_string.rb
ruby-rails-extensions-0.2.0.pre.rc.1 lib/ruby-rails-extensions/extensions/clean_string.rb
ruby-rails-extensions-1.1.0 lib/ruby-rails-extensions/extensions/clean_string.rb
ruby-rails-extensions-1.1.0.pre.rc.1 lib/ruby-rails-extensions/extensions/clean_string.rb