What is the difference between #encode and #force_encoding in ruby?

encoding, ruby

Solution

Difference is pretty big. `force_encoding` sets given string encoding, but does not change the string itself, i.e. does not change it representation in memory:

'łał'.bytes #=> [197, 130, 97, 197, 130]
'łał'.force_encoding('ASCII').bytes #=> [197, 130, 97, 197, 130]
'łał'.force_encoding('ASCII')   #=> "\xC5\x82a\xC5\x82"

`encode` assumes that the current encoding is correct and tries to change the string so it reads same way in second encoding:

'łał'.encode('UTF-16') #=> 'łał'
'łał'.encode('UTF-16').bytes #=> [254, 255, 1, 65, 0, 97, 1, 66] 

In short, `force_encoding` changes the way string is being read from bytes, and `encode` changes the way string is written without changing the output (if possible)

Problem

I really do not understand the difference between `#encode` and `#force_encoding` in Ruby for the `String` class. I understand that `"kam".force_encoding("UTF-8")` will force `"kam"` to be in UTF-8 encoding, but how is `#encode(encoding)` different? http://ruby-doc.org/core-2.0/String.html#method-i-encoding

Original source