swapnesh swapnesh - 2 years ago 86
Ruby Question

How to create a symbol from a string that has whitespaces?

I am creating a Ruby hash for movie names storage.

When the hash's keys are strings that contains whitespaces, it works just fine.

As in:

movies = {"Avatar" => 5, "Lord of the rings" => 4, "Godfather" => 4}

Now I am trying to replace the use of strings with symbols:

movies = {Avatar: 5, Lord of the rings: 4, Godfather: 4}

Obviously that doesn't work.

How does Ruby handle whitespaces in symbol naming?

Answer Source

Try by yourself

"Lord of the rings".to_sym
#=> :"Lord of the rings"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download