I've actually figured out what causes this error, but Googling for it was unsuccessful so I thought I'd write it down here to help out other people. This error pops up when you've got an # -*- coding: undecided -*- comment at the top of one of your files. Emacs added this automatically for me, but re-saving the file caused it to be changed to the correct # -*- coding: utf-8 -*-.
This error pops up when you've got an # -*- coding: undecided -*- comment at the top of one of your files. Emacs added this automatically for me, but re-saving the file caused it to be changed to the correct # -*- coding: utf-8 -*-.
Related
I have an data-file stored with utf-8 encode, and I want to embed the data to an erb template. The data-file is explicitly encoded with utf-8 at the top. But while running the erb engine but I encounter Encoding::CompatibilityError Error.
I thought as the default encoding in Ruby is ASCII, the erb template must also encoded under ascii. I have explicitly changed it to utf-8 but there is no good.
Here is the data-file:
# coding: utf-8
samples: [
{ name: '北京', city: '北京' }
]
Here is the Erb template:
<% # -*- coding: UTF-8 -*- %>
#...
<p><%= samples[:name] %></p>
(I decided to write different answer)
Two issues, I think.
datafile encoding on input
how you output
The erb library knows about the encoding specification in magic comments, but the data file part, you need to take care by yourself. So, when you read the file, you have to specify encoding, or specify default encoding beforehand.
On output, you need to specify the encoding for output. You can specify per I/O channel basis.
To specify default encoding (easiest), you can:
Encoding.default_external = "UTF-8"
to use UTF-8 for all I/O.
In the scenario where you have an ERB template rendering strings from another file that is in UTF-8, adding the following to the top of the ERB template solved it for me:
<%# coding: UTF-8 %>
(instead of <% # -*- coding: UTF-8 -*- %>)
If you're using Rails, have you configured default encoding, in application.rb? like:
config.encoding = "utf-8"
My Rails (3.2.1) project does not contain any configuration other than that.
Other thing you want to check is, whether your datafile really in UTF-8 or not.
If you're using Unix-like system, you can use 'nkf' command to check the code, by:
nkf --guess FILE_NAME
Specify <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> in the header of the template
On top of my Vagrantfile I have:
# -*- mode: ruby -*-
# vi: set ft=ruby :
But every time when I open the file, I have to select manually in SublimeText 3 that I wanna set the syntax to Ruby, so it'll be readable.
How can you set a filename to be opened as a ruby-file in this example. Because the file has no extension.
Your use case is exactly what ApplySyntax is written to address. In fact, I believe Vagrantfile is one of the default "samples", so you might not have to do anything. Also, comments that are handled in some special way on some editors does not apply to all editors (in this case, ST).
I am using the Community Edition of PyCharm.
When I put a break point in my python file and try to debug, always file cp1252.py opens and it goes to this line:
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
Do I need to do any specific setting for debugger in PyCharm?
Thanks for the help.
It turned out that I needed to force the source file to be opened as UTF-8 by adding this at the first line of my code:
# -*- coding: UTF-8 -*-
File.exist? in not working with directory name having special characters. for something like given below
path = "/home/cis/Desktop/'El%20POP%20que%20llevas%20dentro%20Vol.%202'/*.mp3"
it works fine but if it has letters like ñ its returns false.
Plz help with this.
Try the following:
Make sure you're running 1.9.2 or greater and put # encoding: UTF-8 at the top of your file (which must be in UTF-8 and your editor must support it).
If you're running MRI(i.e. not JRuby or other implementation) you can add environment variable RUBYOPT=-Ku instead of # encoding: UTF-8 to the top of each file.
I have an data-file stored with utf-8 encode, and I want to embed the data to an erb template. The data-file is explicitly encoded with utf-8 at the top. But while running the erb engine but I encounter Encoding::CompatibilityError Error.
I thought as the default encoding in Ruby is ASCII, the erb template must also encoded under ascii. I have explicitly changed it to utf-8 but there is no good.
Here is the data-file:
# coding: utf-8
samples: [
{ name: '北京', city: '北京' }
]
Here is the Erb template:
<% # -*- coding: UTF-8 -*- %>
#...
<p><%= samples[:name] %></p>
(I decided to write different answer)
Two issues, I think.
datafile encoding on input
how you output
The erb library knows about the encoding specification in magic comments, but the data file part, you need to take care by yourself. So, when you read the file, you have to specify encoding, or specify default encoding beforehand.
On output, you need to specify the encoding for output. You can specify per I/O channel basis.
To specify default encoding (easiest), you can:
Encoding.default_external = "UTF-8"
to use UTF-8 for all I/O.
In the scenario where you have an ERB template rendering strings from another file that is in UTF-8, adding the following to the top of the ERB template solved it for me:
<%# coding: UTF-8 %>
(instead of <% # -*- coding: UTF-8 -*- %>)
If you're using Rails, have you configured default encoding, in application.rb? like:
config.encoding = "utf-8"
My Rails (3.2.1) project does not contain any configuration other than that.
Other thing you want to check is, whether your datafile really in UTF-8 or not.
If you're using Unix-like system, you can use 'nkf' command to check the code, by:
nkf --guess FILE_NAME
Specify <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> in the header of the template