I can't string.sub!("π", "3.141592654")

I would like to sub in the value of pi for it’s symbol and I am having
trouble doing so.

Any suggestions?

code is simple

string.sub!(“π”,3.141592654")

Thanks for your help

Unless you are using ruby 2.1, you cannot type literal
multi-byte characters in your program’s source code or else you will get
this error:

invalid multibyte char (US-ASCII)

That is because by default ruby expects that the the stuff you
type into your program will consist of ASCII characters only i.e.
each character will be one byte long.

However, you can change what ruby expects by using an
encoding comment at the top of your file(has to be first
or second line):

encoding = UTF-8

That comment warns ruby that each character in your program file
may occupy more than one byte, so ruby should be on the lookout
for multibyte characters, e.g. the one for pi:

encoding = UTF-8

str = “\u03C0” #Unicode escape sequence for pi.
#Ruby automatically converts to UTF-8 encoding.

str.sub!(‘π’, ‘3.14’) #To type the literal character for pi in the
#program’s source code, the encoding of
#source code must be changed
#from ASCII to UTF-8
puts str