简体   繁体   English

ruby watir-webdriver在哈希中使用变量

[英]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;"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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