forked from lobsters/lobsters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonkey.rb
47 lines (40 loc) · 959 Bytes
/
monkey.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module ActiveRecord
class Base
def self.q(str)
ActiveRecord::Base.connection.quote(str)
end
def q(str)
ActiveRecord::Base.connection.quote(str)
end
end
end
class String
def forcibly_convert_to_utf8
begin
if self.encoding.to_s == "UTF-8" && self.valid_encoding?
return self
end
str = self.dup.force_encoding("binary").encode(
"utf-8",
:invalid => :replace,
:undef => :replace,
:replace => "?"
)
if !str.valid_encoding? || str.encoding.to_s != "UTF-8"
raise Encoding::UndefinedConversionError
end
rescue Encoding::UndefinedConversionError
str = self.chars.map {|c|
begin
c.encode("UTF-8", :invalid => :replace, :undef => :replace)
rescue
"?".encode("UTF-8")
end
}.join
if !str.valid_encoding?
raise "still bogus encoding"
end
end
str
end
end