Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 在ruby中返回多个值,用于调用函数_Css_Ruby_Xpath_Capybara_Incompatibility - Fatal编程技术网

Css 在ruby中返回多个值,用于调用函数

Css 在ruby中返回多个值,用于调用函数,css,ruby,xpath,capybara,incompatibility,Css,Ruby,Xpath,Capybara,Incompatibility,是否可以从一个函数返回多个值 我想将返回值传递给另一个函数,我想知道是否可以避免将数组分解为多个值 我的问题? 我正在为我的项目升级水豚,多亏了,我意识到下面的陈述将不再有效 has_selector?(:css, "#rightCol:contains(\"#{page_name}\")") 我想让它以最小的努力工作(有很多这样的情况),所以我想到了使用Nokogiri将css转换为xpath。我想写它,这样上面的函数就可以 has_selector? xpath(:css, "#right

是否可以从一个函数返回多个值

我想将返回值传递给另一个函数,我想知道是否可以避免将数组分解为多个值

我的问题? 我正在为我的项目升级水豚,多亏了,我意识到下面的陈述将不再有效

has_selector?(:css, "#rightCol:contains(\"#{page_name}\")")
我想让它以最小的努力工作(有很多这样的情况),所以我想到了使用Nokogiri将
css
转换为
xpath
。我想写它,这样上面的函数就可以

has_selector? xpath(:css, "#rightCol:contains(\"#{page_name}\")")
但是由于
xpath
必须返回一个数组,所以我需要实际编写这个

has_selector?(*xpath(:css, "#rightCol:contains(\"#{page_name}\")"))
有没有办法得到前一种行为

为了简洁起见,可以假设现在的
xpath
func如下所示

def xpath(*a)
  [1,2]
end

不能让方法返回多个值。为了做您想要做的事情,您必须更改
has\u selector?
,可能是这样的:

alias old_has_selector? :has_selector?
def has_selector? arg
  case arg
  when Array then old_has_selector?(*arg)
  else old_has_selector?(arg)
  end
end

Ruby对从函数返回多个值的支持有限。特别是,当分配给多个变量时,返回的数组将被“解构”:

def foo
  [1, 2]
end

a, b = foo
a #=> 1
b #=> 2   
但是,在您的情况下,您需要使用splat(*)来清楚地表明,您不仅仅是将数组作为第一个参数传递

如果您想要更简洁的语法,为什么不编写自己的包装器:

def has_xpath?(xp)
  has_selector?(*xpath(:css, xp))
end

有趣。lvl比我想要的低了一点,但我猜唯一能让它工作的方法是它只有5-6个位置,所以我只是咬了子弹,采用了第二种形式。水豚本机支持xpath,无需破解:)查看文档了解详细信息。@BillyChan正在将CSS转换为xpath,因为据我所知,2.0没有使用自己的CSS驱动程序。明白了。我已经好几个月没有跟踪水豚了,我已经离开了:)是的,你的表格更干净了,但我想尽量减少变化。起初我只是想转换第二个arg,但希望这样的事情可能会发生,我只是不知道如何。。