如何从具有空格的字符串中创建符号? [英] How to create a symbol from a string that has whitespaces?

查看:73
本文介绍了如何从具有空格的字符串中创建符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为电影名称存储创建一个Ruby哈希.

I am creating a Ruby hash for movie names storage.

当哈希键是包含空格的字符串时,它就可以正常工作.

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

如:

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}

显然那是行不通的.

Ruby如何在符号命名中处理空格?

How does Ruby handle whitespaces in symbol naming?

推荐答案

自行尝试

"Lord of the rings".to_sym
#=> :"Lord of the rings"

这篇关于如何从具有空格的字符串中创建符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆