我正在尝试创建在其中间具有可变性的哈希。我似乎无法将变量视为变量仅作为文字。变量是数组颜色的元素
如
text1 = {:style => 'background-color: variable;'}
我认为这样可行。
text1 = {:style => 'background-color: #{variable};'}
以下是有效的,但它是一种循环方法
text2 = ''
text2 << "background-color: " << variable << ";"
text1 = {:style => text2}
答案 0 :(得分:5)
如果要使用#{}
将变量插入字符串,则需要使用双引号而不是单引号。
variable = "green"
text1 = {:style => "background-color: #{variable};"} #Notice the double quotes
#=> {:style=>"background-color: green;"}