jsjwql
jsjwql

Reputation: 11

Ruby: How to print ®

  1. Save rb file with ASCII, ® can't be right displayed
  2. Save rb file with Unicode, it would cause error Invalid char \357' in expression Invalid char\273' in expression Invalid char `\277' in expression

Upvotes: 0

Views: 2005

Answers (3)

Chris Heald
Chris Heald

Reputation: 62648

If you're displaying this in a web browser, use the ® or ® HTML entities. The browser should interpret them as the correct character.

Upvotes: 0

Morton Fox
Morton Fox

Reputation: 186

You could also try Array#pack.

puts [174].pack('U*')

That won't require any non-ASCII characters in your source code.

Upvotes: 2

Arnaud Le Blanc
Arnaud Le Blanc

Reputation: 99921

You have to declare the source encoding:

# coding: utf-8
p "®"

(just add the # coding: utf-8 line in your file to declare its encoding to utf-8)

Upvotes: 1

Related Questions