Why do I get a Encoding::CompatibilityError with #inspect?
The error message explains already the why:
inspected result must be ASCII only or use the default external encoding
In this case the inspect-command gets a UTF-8 character (Not ASCII), but the default encoding seems to be another.
The default encoding can be read in Encoding.default_external
.
To avoid the error you must encode the result of inspect:
#encoding: utf-8
class Text
def initialize(txt)
@txt = txt
end
def inspect
#force ASCII and replace invalid/undefined characters
("<Text: %s>" % @txt).encode('ASCII', :undef => :replace, :invalid => :replace)
end
end
p Text.new('Hä, was soll das?') #-> <Text: H?, was soll das?>
Instead of ASCII
in encode
you can use also Encoding.default_external
:
("<Text: %s>" % @txt).encode(Encoding.default_external, :undef => :replace)
Encoding::CompatibilityError in rails
I got similar errors before and it turned out that the file itself that contains the code is not using UTF8 encoding :(, so you'd better check the editor you are using as it might not be using UTF8 encoded files.
Encoding::CompatibilityError in Ruby string operator
It's an encoding issue. Your encode_to_s
method returns a binary string:
BCD.encode_to_s(99) #=> "\x99"
BCD.encode_to_s(99).encoding #=> #<Encoding:ASCII-8BIT>
Ruby's string literal on the other hand, creates UTF-8 encoded strings:
s = ""
s.encoding #=> #<Encoding:UTF-8>
This is probably not what you want. You can call String#b
to create a binary string:
s = "".b
s.encoding #=> #<Encoding:ASCII-8BIT>
Appending other binary strings should work without problems.
Ruby Gem randomly returns Encoding Error
In the Query
class there is this line:
@key = Array(key).pack('N')
This creates a String with an associated encoding of ASCII-8BIT (i.e. it’s a binary string).
Later @key
gets used in this line:
query = @sock.send("\xFE\xFD\x00\x01\x02\x03\x04" + @key, 0)
In Ruby 2.0 the default encoding of String literals is UTF-8, so this is combining a UTF-8 string with a binary one.
When Ruby tries to do this it first checks to see if the binary string only contains 7-bit values (i.e. all bytes are less than or equal to 127, with the top byte being 0), and if it does it considers it compatible with UTF-8 and so combines them without further issue. If it doesn’t, (i.e. if it contains bytes greater than 127) then the two strings are not compatible and an Encoding::CompatibilityError
is raised.
Whether an error is raised depends on the contents of @key
, which is initialized from a response from the server. Sometimes this value happens to contain only 7-bit values, so no error is raised, at other times there is a byte with the high bit set, so it generates an error. This is why the errors appear to be “random”.
To fix it you can specify that the string literal in the line where the two strings are combined should be treated as binary. The simplest way would be to use force_encoding
like this:
query = @sock.send("\xFE\xFD\x00\x01\x02\x03\x04".force_encoding(Encoding::ASCII_8BIT) + @key, 0)
Encoding::CompatibilityError in Observations when access active directory from rails
In your example @temp_search
should be an array of users that meet your search criteria. Each one of those users is a Net::LDAP::Entry
object. On those objects you can call methods are available corresponding to the users attributes.
If only one user is returned, the Net::LDAP::Entry
object will still be inside an array. In that case you could call something like:
@temp_search.first.cn
You can also call:
@temp_search.first.attribute_names
to see all available attributes for that object.
For example, you could do something like:
<% @temp_search.each do |user| %>
#call user attributes
user.cn
user.memberof
#etc, other attributes
<% end %>
incompatible character encodings: ASCII-8BIT and UTF-8
I have a suspicion that you either copy/pasted a part of your Haml template into the file, or you're working with a non-Unicode/non-UTF-8 friendly editor.
See if you can recreate that file from the scratch in a UTF-8 friendly editor. There are plenty for any platform and see whether this fixes your problem. Start by erasing the line with #content
and retyping it manually.
Related Topics
Integrate Shoes into Aptana Studio Radrails
Ruby: Why Are All Coordinates Getting Updated in an Array of Arrays
How to Reset Boolean to "Default: False" at End of Day
How to Create Two Routes in One Block in Grape
Mechanize and Ntlm Authentication
Use [].Replace to Make a Copy of an Array
Shading Mask Algorithm for Radiation Calculations
How to Iterate Through This JSON Document Using Ruby
Run Ruby Script That Is Stored on Internet
Generating Devise Controllers - Rails Devise
Ruby 2.1 with Erubis Template Engine
How to Use Multiple Models for Tag_Cloud
Why Does Ruby '**' Operator Have Higher Precedence Than Unary '-'
Rspec -- Test If Method Called Its Block Parameter
Gem::Ext::Builderror: Error: Failed to Build Gem Native Extension. on Cenos 6.5
Error: Null Value in Column "Id" Violates Not-Null Constraint