Skip to content

Instantly share code, notes, and snippets.

@jeffyip
Created October 14, 2012 21:39
Show Gist options
  • Save jeffyip/3889897 to your computer and use it in GitHub Desktop.
Save jeffyip/3889897 to your computer and use it in GitHub Desktop.
# 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
require 'csv'
require 'fastercsv'
if CSV.const_defined?(:Reader)
class CSVBridge < FasterCSV
end
else
class CSVBridge < CSV
end
end
# See http://stackoverflow.com/questions/8268778/rails-2-3-9-encoding-of-query-parameters
# See https://rails.lighthouseapp.com/projects/8994/tickets/4807
# See http://jasoncodes.com/posts/ruby19-rails2-encodings (thanks for the following code, Jason!)
def force_utf8_params
traverse = lambda do |object, block|
if object.kind_of?(Hash)
object.each_value { |o| traverse.call(o, block) }
elsif object.kind_of?(Array)
object.each { |o| traverse.call(o, block) }
else
block.call(object)
end
object
end
force_encoding = lambda do |o|
RubyBridge.force_utf8_encoding(o)
end
traverse.call(params, force_encoding)
end
group :development do
gem 'ruby-debug', :platforms => :ruby_18
gem 'debugger', :platforms => :ruby_19
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment