Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert bullet character in a Ruby string?

Tags:

ruby

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?

like image 523
croceldon Avatar asked Aug 14 '12 15:08

croceldon


People also ask

How do you add a variable to a string in Ruby?

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.

How do I use a string in Ruby?

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.

Can you index a string in Ruby?

Ruby also allows you to index strings using a Range or a Regexp object as well.


1 Answers

You can put the Unicode character in there.

str = "\u2022" + " hello world"
like image 101
Larsenal Avatar answered Sep 30 '22 05:09

Larsenal