Created
November 7, 2009 01:42
-
-
Save lsegal/228455 to your computer and use it in GitHub Desktop.
Get MySQL to use UTF-8 in Ruby1.9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mysql' | |
class Mysql::Result | |
def encode(value, encoding = "utf-8") | |
String === value ? value.force_encoding(encoding) : value | |
end | |
def each_utf8(&block) | |
each_orig do |row| | |
yield row.map {|col| encode(col) } | |
end | |
end | |
alias each_orig each | |
alias each each_utf8 | |
def each_hash_utf8(&block) | |
each_hash_orig do |row| | |
row.each {|k, v| row[k] = encode(v) } | |
yield(row) | |
end | |
end | |
alias each_hash_orig each_hash | |
alias each_hash each_hash_utf8 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment