Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.force_encoding() in Ruby 1.8.7 (or Rails 2.x)

Is there a solution to use String.force_encoding() in Ruby 1.8.7 (or Rails 2.x) so that it works like in Ruby 1.9? I read something about require active_support, but this does not work

$> gem list --local | grep 'rails\|activesupport'

 activesupport (3.0.3, 2.3.8, 2.3.5)
 rails (2.3.8, 2.3.5)

$> ruby -v

ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin10.4.0]

$> rails -v

Rails 2.3.8

irb:

> require "rubygems"
=> true 
> require "active_support"
=> true 
> "asdf".force_encoding("UTF-8")
NoMethodError: undefined method `force_encoding' for "asdf":String
> String.respond_to?(:force_encoding)
=> false
like image 667
f00860 Avatar asked Jan 03 '11 11:01

f00860


1 Answers

This will give you String#to_my_utf8 in both Ruby 1.8.7 and Ruby 1.9:

require 'iconv'
class String
  def to_my_utf8
    ::Iconv.conv('UTF-8//IGNORE', 'UTF-8', self + ' ')[0..-2]
  end
end

And then...

?> "asdf".to_my_utf8
=> "asdf"

Inspired by Paul Battley and also remembering some of my older work on the remote_table gem.

like image 74
Seamus Abshere Avatar answered Sep 25 '22 09:09

Seamus Abshere