coolercargo
coolercargo

Reputation: 247

ruby watir-webdriver using a variable in a hash

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

Answers (1)

Justin Ko
Justin Ko

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

Related Questions