使用变量作为键访问Ruby哈希
发布时间:2020-12-16 19:13:08 所属栏目:百科 来源:网络整理
导读:如果我有以下 ruby哈希: environments = { 'testing' = '11.22.33.44','production' = '55.66.77.88'} 我如何访问上述哈希的部分内容?下面举例说明我想要实现的目标. current_environment = 'testing'"rsync -ar root@#{environments[#{testing}]}:/htdocs
如果我有以下
ruby哈希:
environments = { 'testing' => '11.22.33.44','production' => '55.66.77.88' } 我如何访问上述哈希的部分内容?下面举例说明我想要实现的目标. current_environment = 'testing' "rsync -ar root@#{environments[#{testing}]}:/htdocs/" 解决方法
看起来你想执行最后一行,因为它显然是一个shell命令而不是Ruby代码.您不需要插值两次;曾经做过:
exec("rsync -ar root@#{environments['testing']}:/htdocs/") 或者,使用变量: exec("rsync -ar root@#{environments[current_environment]}:/htdocs/") 请注意,更多的Ruby方法是使用符号而不是字符串作为键: environments = { :testing => '11.22.33.44',:production => '55.66.77.88' } current_environment = :testing exec("rsync -ar root@#{environments[current_environment]}:/htdocs/") (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |