Is this missing the test that it doesn't raise an ArgumentError.  I recently wrote something like this for a Rails app I work on, though in rspec

```
# coding: utf-8
require 'spec_helper'
require_dependency Rails.root.join('lib/string_formatter').to_s

describe StringFormatter do
  it '#hash_values_as_utf8' do
    h = {
      :foo => "Schäfbernt\xED",
      'bar' => 1,
      'baz' => :qux,
    }.with_indifferent_access
    expect {
      h[:foo].present?
    }.to raise_error(ArgumentError) {|e|
      e.message == "invalid byte sequence in UTF-8"
    }
    StringFormatter.hash_values_as_utf8!(h)
    expect(h[:foo].present?).to be_true
    expect(h[:foo]).to eq("Schäfbernt")
    expect(h[:bar]).to eq(1)
    expect(h[:baz]).to eq(:qux)
  end
end
```

and the impl

```ruby
# coding: utf-8
module StringFormatter
  module_function

  def hash_values_as_utf8!(hash)
    hash.each do |key, value|
      next unless value.is_a?(String)
      hash[key] = StringFormatter.as_utf8(value, '')
    end
  end

  def as_utf8(str, replace = '_')
    if str.respond_to?(:encode!)
      if defined?(JRUBY_VERSION)
        # don't blow up https://github.com/jruby/jruby/issues/375
        str = as_ascii(str)
      else
        # Converting it to a higher higher character set (UTF-16) and then
        #  back (to UTF-8) ensures that you will strip away invalid or undefined byte sequences.
        str.encode!(Encoding::UTF_16LE, :invalid => :replace, :undef => :replace, :replace => replace)
      end
      str.encode!(Encoding::UTF_8)
    else
      str
    end
  rescue => e
    Airbrake.notify(e)
    force_utf8_encoding(str)
  end

  # help out copy and pasting errors of good-looking email addresses
  # by stripping out non-ASCII characters
  def as_ascii(str)
    if str.respond_to?(:to_ascii)
      str.to_ascii
    else
      # avoids invalid multi-byte escape error
      ascii_text = str.encode( Encoding::ASCII, invalid: :replace, undef: :replace, replace: '' )
      # see http://www.ruby-forum.com/topic/183413
      pattern = Regexp.new('[\x80-\xff]', nil, 'n')
      ascii_text.gsub(pattern, '')
    end
  end
end
```

For 1.8, I have seen something like the below using BINARY aka ASCII-8BIT

```ruby
  # Encodes a string from encoding "from" to encoding "to" in
  # a way that works for both ruby 1.8 and 1.9
  def convert_string_encoding(to, from, str)
    if "1.9".respond_to?(:force_encoding)
      str = str.dup if str.frozen?
      str.encode(to, from, :undef => :replace)
    else
      require 'iconv'
      Iconv.conv(to, from, str)
    end
  end
```