HI,
I have a ruby program that takes activerecord data and outputs it to
XML using the REXML library.
It has been working fine until some accented characters were used in
one of the fields.
I have isolated the error message to this file C:\ruby\lib\ruby
\1.8\rexml\encodings\ICONV.rb and line after the commented out line
below:
=====================
module REXML
module Encoding
def decode_iconv(str)
Iconv.conv(UTF_8, @encoding, str)
end
def encode_iconv(content)
# cag changed to test encoding hack
#Iconv.conv(@encoding, UTF_8, content)
Iconv.conv(@encoding, 'LATIN1', content)
end
register("ICONV") do |obj|
Iconv.conv(UTF_8, obj.encoding, nil)
class << obj
alias decode decode_iconv
alias encode encode_iconv
end
end
end
end
=======================
So I think this has to do with REXML expecting UTF-8 which is fine in
Ruby 1.9 but Ruby 1.8 uses LATIN1 I think. I hacked the file as shown
above and this works, but what I really want to do is override the
"encode_iconv" method in this module in my own code so I am not
changing the core libraries.
I have attempted a couple of things but can't quite get it such as
putting this at the top of my main file before call that uses it:
===========================
include REXML
module REXML
module Encoding
def encode_iconv(content)
Iconv.conv('ISO-8859-1', 'LATIN1', content)
end
end
end
===========================
To test I just created a simple xml document
@doc = Document,new("<xml? version='1.0' encoding='iso-8859' ?
><some text here/>")
... add some elements etc ...
and then this is the call where it invoked:
@doc.write(xml_string,0)
I also tried eval technique from a David Black post and saw some posts
indicating use of self and object reference.
Anyone got an idea? Is is because it is mixed in somewhere when REXML
is loaded and thus my override attempts are not recognized?
Thanks,
Carl
--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---