简体   繁体   中英

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}

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;"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM