Ruby Output Unicode Character

前端 未结 5 1202
不思量自难忘°
不思量自难忘° 2020-12-24 05:18

I\'m not a Ruby dev by trade, but am using Capistrano for PHP deployments. I\'m trying to cleanup the output of my script and am trying to add a unicode check mark as discus

5条回答
  •  悲&欢浪女
    2020-12-24 05:48

    falsetru's answer is incorrect.

    checkmark = "\u2713"
    puts checkmark.encode('utf-8')
    

    This transcodes the checkmark from the current system encoding to UTF-8 encoding. (That works only on a system whose default is already UTF-8.)

    The correct answer is:

    puts checkmark.force_encoding('utf-8')
    

    This modifies the string's encoding, without modifying any character sequence.

提交回复
热议问题