John Smith
John Smith

Reputation: 6259

Unicodes in file get escaped

I have a model Person when i save a new Person through the console with unicode for eg:

p = Person.new
p.name = "Hans"
p.street = "Jo\u00DFstreet"
p.save

It returns on p.street Joßstreet what is correct. But when i try in my seeds to build from a file:

file.txt

Hans;Jo\u00DFstreet
Jospeh;Baiuvarenstreet

and run this in my seed:

File.readlines('file.txt').each do |line|
  f = line.split(';')
  p = Person.new
  p.name = p[0]
  p.street = p[1]
  p.save
end

Now when i call for eg: p = Person.last i get p.street => "Jo\\u00DFstreet"

I dont understand why the \u00DF gets escaped! What can i do to fix this problem? Thanks

Upvotes: 0

Views: 74

Answers (1)

alno
alno

Reputation: 3616

It's because escape sequences such as \u00DF are handled only in source code string literals, as a part of Ruby syntax.

When you read some file (or receive data from somewhere else) Ruby don't try to handle escape sequences and you should do it by your own code.

To unescape string you may use approaches described here or there, but maybe you'd be better to store initially unescaped text in your file.

Upvotes: 1

Related Questions