1

I'm getting the following error with my Ruby 1.9

I tried using (as some resources suggested)

string.force_encoding('utf-8')

But it didn't help!

Any ideas how to resolve this? Is there a way to eliminate such characters before saving to DB? or is a there a way to make them show?

For example, when I want to print:

Opowieść o kulcie przemocy

I get:

Opowie?? o kulcie przemocy
fresskoma
  • 25,481
  • 10
  • 85
  • 128
AHmedRef
  • 2,555
  • 12
  • 43
  • 75
  • 2
    If you are printing it to a terminal, make sure your terminal is also set to UTF-8. If it is a webpage, make sure that has the right encoding too ( http://stackoverflow.com/questions/4696499/which-one-to-use-meta-charset-utf-8-vs-meta-http-equiv-content-type ) – fresskoma Jul 18 '12 at 23:34
  • So you are generating an SQL file? Perhaps your database table has the wrong encoding settings? There is a lot of stuff that can go wrong with SQL databases and encoding ( http://stackoverflow.com/questions/346531/utf8-problem-with-mysql-5 ) – fresskoma Jul 19 '12 at 00:51
  • my encoding settings is 'utf8'; – AHmedRef Jul 19 '12 at 12:55

1 Answers1

1

I make it work using this first line of code:

# encoding: UTF-8

string = "Opowieść o kulcie przemocy"
p string.force_encoding('utf-8')

when i write to the DB , I use the encode and not the force_encoding , for example:

conn.exec(sql.encode("UTF-8"))

where sql is the statement containing the text that needs to be encoded

Franco Rondini
  • 10,841
  • 8
  • 51
  • 77
  • This primer explains very well: [Ruby 1.9 Encodings: A Primer and the Solution for Rails](http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/) – Franco Rondini Jul 31 '12 at 21:48