Reputation: 247
I am trying to create hash that has a vairable in the middle of it. I can't seem to get the variable to be seen as a variable only as a literal. Variable is an element in an array colors
Such as
text1 = {:style => 'background-color: variable;'}
I thought this would work.
text1 = {:style => 'background-color: #{variable};'}
The following works but it is a round-about-approach
text2 = ''
text2 << "background-color: " << variable << ";"
text1 = {:style => text2}
Upvotes: 2
Views: 401
Reputation: 46836
If you want to insert variables into a string using #{}
, then you need to use double quotes instead of single quotes.
variable = "green"
text1 = {:style => "background-color: #{variable};"} #Notice the double quotes
#=> {:style=>"background-color: green;"}
Upvotes: 5