ruby中的大小写条件不';你什么时候进来
我的代码在执行时没有进入case-conditional中的when循环。 我想要的是根据函数的*args来发送两个不同的GET请求。 因此,当我不发送请求中的一个参数时,我可以验证错误。 如果有人有一个更好的逻辑来做这件事的一种方法,我也很感激 这是我的密码:ruby中的大小写条件不';你什么时候进来,ruby,automated-tests,conditional-statements,httparty,Ruby,Automated Tests,Conditional Statements,Httparty,我的代码在执行时没有进入case-conditional中的when循环。 我想要的是根据函数的*args来发送两个不同的GET请求。 因此,当我不发送请求中的一个参数时,我可以验证错误。 如果有人有一个更好的逻辑来做这件事的一种方法,我也很感激 这是我的密码: def get_function(access_token,order1,order2,*args) case args when args = "order1" self.cl
def get_function(access_token,order1,order2,*args)
case args
when args = "order1"
self.class.get("/v1/apiendpoint?order2=#{order2}",
headers: {'accesstoken': "#{access_token}"})
when args = "order2"
self.class.get("/v1/apiendpoint?order1=#{order1}",
headers: {'accesstoken': "#{access_token}"})
end
end
当我使用binding.pry(调试)执行时,它会显示这一部分,而不会执行代码的其余部分
From: C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/cucumber-core-8.0.1/lib/cucumber/core/test/action.rb @ line 25 Cucumber::Core::Test::Action#execute:
22: def execute(*args)
23: @timer.start
24: @block.call(*args)
==> 25: passed
26: rescue Result::Raisable => exception
27: exception.with_duration(@timer.duration)
28: rescue Exception => exception
29: failed(exception)
30: end
这里有多个问题:
case args
when args = "order1"
首先,args
是一个数组
——因此它不可能等于字符串
。我不确定你打算在这里发生什么,所以不能确切地说如何解决它
其次,=
是一个赋值运算符,而=
执行相等检查
最后,这是一个case
语句,而不是if
语句,所以实际上不应该在这里执行相等性检查。。。这两种方法中的任何一种在句法上都是有意义的:
case args
when "order1"
# ...
end
# OR:
case
when args == "order1"
# ...
end
另外,请注意,您的问题描述有点混乱。你说: when循环
但这不是一个循环。您可以称之为“子句”或“语句”,但它肯定不是“循环”。根据Tom的帮助,决定使用
IF
语句
以下是有效的方法:
def get_function(access_token,order1,order2,*args)
if args == ["order1"]
self.class.get("/v1/apiendpoint?order2=#{order2}",
headers: {'accesstoken': "#{access_token}"})
else
self.class.get("/v1/apiendpoint?order1=#{order1}",
headers: {'accesstoken': "#{access_token}"})
end
args
是一个参数的数组
,因此将其与字符串
进行比较时,无论字符串是还是,其计算结果总是false
我不知道在函数的行为方面需要什么,但我可以说的是,如果您想查看args
数组
以将每个参数与字符串
进行比较,那么在数组上迭代可能是一个更好的主意
带有的示例,如果:
def args_example(*args)
# Working directly with *args doesn't work, so we assign it to arguments
arguments = *args
# We need a way to save the output after the if clause
output = []
# Let's iterate!
arguments.each do |argument|
# This is where the if would come in
if argument == "One"
output << 1
elsif argument == "Two"
output << 2
else
output << 0
end
end
output
end
def args_example(*args)
# Working directly with *args doesn't work, so we assign it to arguments
arguments = *args
# We need a way to save the output after the case clause
output = []
# Let's iterate!
arguments.each do |argument|
# This is where the case would come in
case argument
when "One"
output << 1
when "Two"
output << 2
else
output << 0
end
end
output
end
带有案例的示例
:
def args_example(*args)
# Working directly with *args doesn't work, so we assign it to arguments
arguments = *args
# We need a way to save the output after the if clause
output = []
# Let's iterate!
arguments.each do |argument|
# This is where the if would come in
if argument == "One"
output << 1
elsif argument == "Two"
output << 2
else
output << 0
end
end
output
end
def args_example(*args)
# Working directly with *args doesn't work, so we assign it to arguments
arguments = *args
# We need a way to save the output after the case clause
output = []
# Let's iterate!
arguments.each do |argument|
# This is where the case would come in
case argument
when "One"
output << 1
when "Two"
output << 2
else
output << 0
end
end
output
end
这是检查提供给函数的所有参数的一种方法(当然还有一种更短的方法),并相应地发送GET
请求
干杯!我想当我回答这个问题时,我还没有完全理解。您正在尝试检查用户是否为参数order1
提供了值,或者用户是否为函数提供了字符串“order1”
作为参数?嘿…第二个选项。如果它收到“order1”
做点什么……好吧,我的答案在概念上是适用的,因为我正在检查字符串
相等:)