2012-07-13 79 views
0

Ruby新手,需要一些帮助。将变量传递给select_list,我想查找与变量匹配的第一个选项。watir select_list正则表达式变量NoValueFoundException

列表选项有:

  • 波特兰
  • 波特兰,ME
  • 波特兰,俄勒冈

...我分配城市可变

var="Portland" 

使用: f.select_list(:id => /location/).select(/var/)

NoValueFoundException

但是当我使用...

`f.select_list(:id => /location/).select(/Portland/) #no exception, gives 1st entry` 

如何访问使用正则表达式和变量的第一个选项?

回答

0

而不是使用文字正则表达式/ var /,您需要从变量var的字符串值构造正则表达式。

实施例:

:001 > /hello/.match('hello world') 
=> #<MatchData "hello"> 
:002 > exp = 'hello' 
=> "hello" 
:003 > /exp/.match('hello world') 
=> nil 
:004 > Regexp.new(exp).match('hello world') 
=> #<MatchData "hello">