【发布时间】:2017-06-08 20:45:11
【问题描述】:
如何从以下代码中删除“警告:`*' 解释为参数前缀”?
hash = {"a" => 1,
"b" => 2,
"s" => 3,}
if "string".start_with? *hash.keys then
puts "ok"
else
puts "ng"
end
当我运行上面的代码时,我得到:
$ ruby -w /tmp/a.rb
/tmp/a.rb:5: warning: `*' interpreted as argument prefix
ok
解决此警告的最佳方法是什么?
我试着像这样在hash 周围加上括号:
hash = {"a" => 1,
"b" => 2,
"s" => 3,}
if "string".start_with? (*hash.keys) then
puts "ok"
else
puts "ng"
end
然后你得到:
$ ruby -w /tmp/a.rb
/tmp/a.rb:5: syntax error, unexpected *
if "string".start_with? (*hash.keys) then
^
/tmp/a.rb:5: syntax error, unexpected ')', expecting '='
if "string".start_with? (*hash.keys) then
^
/tmp/a.rb:7: syntax error, unexpected keyword_else, expecting end-of-input
这是Why does white-space affect ruby function calls? 中描述的问题,显然不是修复我试图修复的警告的方法。
我的红宝石版本是:
$ ruby --version
ruby 2.3.3p222 (2016-11-21) [x86_64-linux-gnu]
【问题讨论】:
-
@AlexGolubenko,这个问题是关于参数前缀警告,而不是函数名称和括号之间的空格。
-
我同意雅舒师的观点。人们在堆栈溢出上找到答案的主要方法是谷歌警告。如果他们有这个问题,他们不会用谷歌搜索
why does white space affect ruby function calls,因为这样做意味着他们已经知道是什么导致了警告。我认为应该删除重复的标签。