International chars using RSpec with Ruby on Rails

前端 未结 2 1619
梦如初夏
梦如初夏 2021-02-14 00:24

I have just started using RSpec and I copied the very simple test on the RSpec github repo just to make sure things are working as expected:

require \'spec_helpe         


        
2条回答
  •  你的背包
    2021-02-14 00:30

    International characters almost always use values outside of the range of US-ASCII, which is only the english alphabet, numbers and a small set of symbols that you find on your keyboard (if you use a US keyboard). Characters with accents, fanciness, or that aren't even characters (eg. emoticons) are represented in more than one byte, which is all that is used to represent US-ASCII. The mappings of numerical value to character is callen an encoding. After US-ASCII, there's ISO-8891-1, which adds accents to the file (mostly Spanish, French, Swedish, etc.) (eg: é, å, ü, etc.). After that, you get Unicode, which includes things like ˝, ‰, Ó, ˆ, ◊, or almost any symbol you can think of in any language.

    Ruby, by default, has the encoding of a program and all of the strings in it as US-ASCII. You can either change the encoding of the entire file (and everything in it) with the magic comment (see @KL-7's answer) or you can change it on a string by string basis:

    "Olé".force_encoding("ISO-8891-1")
    

    Ruby also supports an imaginary encoding called ASCI 8-bit, which is essentially binary data with no encoding.

提交回复
热议问题