Ruby:如何使用/将多单词字符串转换为符号

Ruby:如何使用/将多单词字符串转换为符号,ruby,hash,key,symbols,Ruby,Hash,Key,Symbols,大多数散列键都是单字名称。让我们以电影名称和收视率为例。制作一个符号很容易: my_hash={Ferris_Bueller:5} 如果我不想在键名中使用下划线,但需要该字符串作为符号,该怎么办? 我曾想过这样做,但没用: my_hash={“Ferris Bueller”:5} 问题是,我试图提示用户命名一部电影,对其进行评级,然后将这些值作为键/值对添加到my\u hash中。人们通常会输入两个单词的电影,没有下划线。如何将该密钥作为符号存储在哈希中?即: 将“命名电影:” name=get

大多数散列键都是单字名称。让我们以电影名称和收视率为例。制作一个符号很容易:

my_hash={Ferris_Bueller:5}

如果我不想在键名中使用下划线,但需要该字符串作为符号,该怎么办? 我曾想过这样做,但没用:

my_hash={“Ferris Bueller”:5}

问题是,我试图提示用户命名一部电影,对其进行评级,然后将这些值作为键/值对添加到
my\u hash
中。人们通常会输入两个单词的电影,没有下划线。如何将该密钥作为符号存储在哈希中?即:

将“命名电影:”
name=gets.chomp
将“评级为1-5:”
rating=get.chomp
my_hash[name.to_sym]=评级


用户键入两个单词的电影,例如
Robo Cop
。为什么这不起作用?

为了让它起作用,你可能需要使用散列火箭

my_hash = { :"Ferris Bueller" => 5}   

为了让这项工作,你可能需要使用散列火箭实际上

my_hash = { :"Ferris Bueller" => 5}   

为了让这项工作,你可能需要使用散列火箭实际上

my_hash = { :"Ferris Bueller" => 5}   

为了让这项工作,你可能需要使用散列火箭实际上

my_hash = { :"Ferris Bueller" => 5}   

你也可以使用.gsub!将u转换为空格,然后.gsub!再次将其转换回。这样,代码仍然被注册,但用户从未看到下划线。下面是一个例子:

movies = {
The_Raven: 4,
I_Robot: 3
}

puts "Howdy, please select one of the following."
choice = gets.chomp
case choice
when "update"
puts "Please select which movie you'd like to update."
title = gets.chomp
title.gsub!(/ /, "_")
#By doing this, the user can type 'The Raven' and Ruby will be able to
#register it as a title in your movies hash.
if
    movies[title.to_sym].nil?
    puts "That movie doesn't exist in our inventory!"
else 
    puts "What would you like to change the rating to? (Rating must be 1 - 5)"
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
    title.gsub!(/_/, " ")
#Then you convert it back from an underscore to a space. This way Ruby 
#will display to the user the title without underscores. 
    puts "#{title} has been updated with a new rating of #{rating}."
end`enter code here`

你也可以使用.gsub!将u转换为空格,然后.gsub!再次将其转换回。这样,代码仍然被注册,但用户从未看到下划线。下面是一个例子:

movies = {
The_Raven: 4,
I_Robot: 3
}

puts "Howdy, please select one of the following."
choice = gets.chomp
case choice
when "update"
puts "Please select which movie you'd like to update."
title = gets.chomp
title.gsub!(/ /, "_")
#By doing this, the user can type 'The Raven' and Ruby will be able to
#register it as a title in your movies hash.
if
    movies[title.to_sym].nil?
    puts "That movie doesn't exist in our inventory!"
else 
    puts "What would you like to change the rating to? (Rating must be 1 - 5)"
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
    title.gsub!(/_/, " ")
#Then you convert it back from an underscore to a space. This way Ruby 
#will display to the user the title without underscores. 
    puts "#{title} has been updated with a new rating of #{rating}."
end`enter code here`

你也可以使用.gsub!将u转换为空格,然后.gsub!再次将其转换回。这样,代码仍然被注册,但用户从未看到下划线。下面是一个例子:

movies = {
The_Raven: 4,
I_Robot: 3
}

puts "Howdy, please select one of the following."
choice = gets.chomp
case choice
when "update"
puts "Please select which movie you'd like to update."
title = gets.chomp
title.gsub!(/ /, "_")
#By doing this, the user can type 'The Raven' and Ruby will be able to
#register it as a title in your movies hash.
if
    movies[title.to_sym].nil?
    puts "That movie doesn't exist in our inventory!"
else 
    puts "What would you like to change the rating to? (Rating must be 1 - 5)"
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
    title.gsub!(/_/, " ")
#Then you convert it back from an underscore to a space. This way Ruby 
#will display to the user the title without underscores. 
    puts "#{title} has been updated with a new rating of #{rating}."
end`enter code here`

你也可以使用.gsub!将u转换为空格,然后.gsub!再次将其转换回。这样,代码仍然被注册,但用户从未看到下划线。下面是一个例子:

movies = {
The_Raven: 4,
I_Robot: 3
}

puts "Howdy, please select one of the following."
choice = gets.chomp
case choice
when "update"
puts "Please select which movie you'd like to update."
title = gets.chomp
title.gsub!(/ /, "_")
#By doing this, the user can type 'The Raven' and Ruby will be able to
#register it as a title in your movies hash.
if
    movies[title.to_sym].nil?
    puts "That movie doesn't exist in our inventory!"
else 
    puts "What would you like to change the rating to? (Rating must be 1 - 5)"
    rating = gets.chomp
    movies[title.to_sym] = rating.to_i
    title.gsub!(/_/, " ")
#Then you convert it back from an underscore to a space. This way Ruby 
#will display to the user the title without underscores. 
    puts "#{title} has been updated with a new rating of #{rating}."
end`enter code here`

您可以使用以下符号制作多字符号:

:"multi word symbol" => value

您可以使用以下符号制作多字符号:

:"multi word symbol" => value

您可以使用以下符号制作多字符号:

:"multi word symbol" => value

您可以使用以下符号制作多字符号:

:"multi word symbol" => value

为什么这不管用?
真管用……难以置信。我只是在使用repl.it,它在这方面给了我一个错误。再试一次,效果很好。我就是不明白。谢谢你,八月。
为什么这个不行?
它真的行……难以置信。我只是在使用repl.it,它在这方面给了我一个错误。再试一次,效果很好。我就是不明白。谢谢你,八月。
为什么这个不行?
它真的行……难以置信。我只是在使用repl.it,它在这方面给了我一个错误。再试一次,效果很好。我就是不明白。谢谢你,八月。
为什么这个不行?
它真的行……难以置信。我只是在使用repl.it,它在这方面给了我一个错误。再试一次,效果很好。我就是不明白。谢谢你,奥古斯特。我也试过了,而且确实有效。我使用的原始代码也可以工作。一定是repl.it引擎出了故障或其他什么问题。我正在学习刷新很多东西。我也尝试过这个,而且确实有效。我使用的原始代码也可以工作。一定是repl.it引擎出了故障或其他什么问题。我正在学习刷新很多东西。我也尝试过这个,而且确实有效。我使用的原始代码也可以工作。一定是repl.it引擎出了故障或其他什么问题。我正在学习刷新很多东西。我也尝试过这个,而且确实有效。我使用的原始代码也可以工作。一定是repl.it引擎出了故障或其他什么问题。我正在学着刷新很多东西。