2

I have a method I would like to use for testing that just makes a GET request to a specific url. The parameter for the url is just one parameter with multiple values that can be passed seperated by commas.

an ideal request should have the url look like : url + "/items?item_ids?=1,2,3,4"

My method accepts multiple arguments:

def method(*args) url = url + "/items?item_ids?=#{args}" end

The issue I am having is that it will input the args into the url as an array which does not work. Is there a good way to add the arguments to the url not as an array and only seperated by commas? There will be an unknown number of arguments passed into this method.

2 Answers 2

2

You're basically using string interpolation on an array of arguments. You can easily change the array into a string of arguments separated by commas by doing

def method(*args)
  url = url + "/items?item_ids?=#{args.join(',')}"
end

Get me 99 slayer on Runescape bro ;)

Sign up to request clarification or add additional context in comments.

1 Comment

Wow that was very simple and I was overthinking this. Thanks for the help. Will see what I can do for Runescape haha
0

Going by the standard rails convention, and assuming items goes to ItemsController#index action, you can do

#/items?item_ids?=1,2,3,4
def index 
  item_id_list = params[:item_ids]
end

At this point, item_id_list will become a comma seperated string "1,2,3,4" and you can perform any string interpolation fuctions.

E.g if you want all the ids to an array, you can do

item_id_list.split(",") #=> [1,2,3,4] etc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.