Ruby incompatible character encodings

I'm currently trying to write a script that iterates over the input file and validates the data on the website. If he finds new data, he prints out the terminal, which he passes if he does not tell me that he is failing. And vice versa for deleted data. It worked fine until the input file that I received does not contain the β€œβ„’β€ character. Then, when the ruby ​​falls on this line, he spits out an error:

PDAPWeb.rb: 73: in `include? ': incompatible character encodings: UTF-8 and IBM437 (Encoding :: CompatibilityError)

An insult line is a simple check to see if text exists on a page.

if browser.text.include? (program_name)

Where the variable program_name is the analyzed part of the information from the input file. In this case, program_name contains the symbol "TM" mentioned earlier.

After some research, I found that adding the encoding of the line #: utf-8 to the beginning of my script might help, but it still has not been useful.

I added this to my program_name variable to see if it would help (and this allowed my script to work without errors), but now it incorrectly finds the TM character when it should be.

program_name = record[2].gsub("\n", '').force_encoding("utf-8").encode("IBM437", replace: nil)

It seemed that he was transforming the TM symbol into this: ΓÀó

I thought maybe I had parts of IBM437 and utf-8, so I tried the opposite

program_name = record[2].gsub("\n", '').force_encoding("IBM437").encode("utf-8", replace: nil)

and now I get this error when trying to run a script

PDAPWeb.rb: 48: in `encode ': U + 2122 from UTF-8 to IBM437 (Encoding :: UndefinedConve rsionError)

ruby ​​1.9.3p392 (2013-02-22), , , , .

?

+4
1

. β„’, UTF-8. , , , Ruby , IBM437 ( Windows).

:

>> input = "β„’"
=> "β„’"
>> input.encoding
=> #<Encoding:UTF-8>
>> input.force_encoding 'ibm437'
=> "\xE2\x84\xA2"

, force_encoding , . , , ( ).

- β„’ UTF-8, Ruby (Watir, , ):

>> web_page = 'β„’'
=> "β„’"
>> web_page.encoding
=> #<Encoding:UTF-8>

, , , :

>> web_page.include? input
Encoding::CompatibilityError: incompatible character encodings: UTF-8 and IBM437
    from (irb):11:in `include?'
    from (irb):11
    from /Users/matt/.rvm/rubies/ruby-2.2.1/bin/irb:11:in `<main>'

ASCII (.. , 128), . UTF-8 IBM437 ASCII , ASCII. , β„’.

, Ruby, . :

>> input.force_encoding 'utf-8'
=> "β„’"

, , . ( , ):

input = File.read("input_file.txt", :encoding => "utf-8")
# now input will be in the correct encoding

, , - , Ruby .

:

>> web_page.include? input
=> true

encode . , . , UTF-8, IBM437:

>> input.force_encoding("utf-8").encode("IBM437", replace: nil)
Encoding::UndefinedConversionError: U+2122 from UTF-8 to IBM437
    from (irb):16:in `encode'
    from (irb):16
    from /Users/matt/.rvm/rubies/ruby-2.2.1/bin/irb:11:in `<main>'

IBM437 β„’, , , , . Ruby , . :undef, :

>> input.force_encoding("utf-8").encode("IBM437", :undef => :replace)
=> "?"

, force_encoding IBM437, UTF-8, ΓÀó:

>> input.force_encoding("IBM437").encode("utf-8", replace: nil)
=> "ΓÀó"

IBM437 Ruby, force_encoding . UTF-8 β„’ 0xe2 0x84 0xa2, IBM437 , , UTF-8.

( , , . , .)

+6

All Articles