2012-07-13 76 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">