-1

My code when executed it's not entering in the when loop inside the case conditional. What I wanted is to sen two different GET requests based on the *args of the function. So I can validate the errors when I don't send one of the parameters in the request. If someone have a better logic to do it one method, I appreaciate as well.

Here is my code:

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

When I execute with binding.pry (debugging) it shows this part, and doesn't execute the rest of the code.

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

3 Answers3

3

There are multiple problems here:

case args
when  args = "order1"

Firstly, args is an Array - so it cannot possibly be equal to a String. I'm not sure what you intended to happen here, so can't say exactly how to fix it.

Secondly, = is an assignment operator, whereas == performs an equality check.

And lastly, this is a case statement, not an if statement, so you shouldn't actually be performing an equality check here... Either of these would have made sense syntactically:

case args
when "order1"
   # ...
end

# OR:

case
when args == "order1"
  # ...
end

Also, note that your question description is a bit confusing. You said:

the when loop

but this is not a loop. You could call it a "clause", or a "statement", but it's certainly not a "loop".

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
1

Following Tom help, decided to go with IF statement.

Here is what worked:

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
  • I think I hadn't quite understood the question when I answered. Are you trying to check whether the user provided a value for the parameter `order1`, or whether the user provided the **`String`** `"order1"` as a parameter to the function? – Bruncky Jan 15 '21 at 18:44
  • hey...second option. If it receives `"order1"` do something... – Tacio Degrazia Jan 18 '21 at 13:18
  • Alright, so my answer applies conceptually, as I'm checking for `String` equality :) – Bruncky Jan 23 '21 at 14:47
1

args is an Array of arguments, so comparing it to a String will always evaluate to false, no matter the String.

I don't know exactly what you need in terms of the behaviour of the function, but what I can say is that if you want to look inside the args Array to compare every argument to a String, it might be a better idea to iterate over the array.

Example with if:

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
args_example("One", "Two", "Three")

=> [1, 2, 0]

Example with case:

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
args_example("One", "Two", "Three")

=> [1, 2, 0]

This is one way to check all the arguments provided to the function (there's surely a shorter way) and send a GET request accordingly.

Cheers!

NOTE: I saved the output to be able to display it, but I realised that since you're only performing a GET request, you don't need to do that. Simply execute the request instead of saving the output.

Bruncky
  • 117
  • 12