I'm trying to create a Ruby 1.9.3 string with a bullet character in it.
str = "•" + " hello world"
But, when I type it in, I get a syntax error about a non-ASCII character.
How can I do this?
Instead of terminating the string and using the + operator, you enclose the variable with the #{} syntax. This syntax tells Ruby to evaluate the expression and inject it into the string. This is the same program you've already written, but this time we're using string interpolation to create the output.
A string in Ruby is an object (like most things in Ruby). You can create a string with either String::new or as literal (i.e. with the double quotes "" ). But you can also create string with the special %() syntax With the percent sign syntax, the delimiters can be any special character.
Ruby also allows you to index strings using a Range or a Regexp object as well.
You can put the Unicode character in there.
str = "\u2022" + " hello world"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With