0

I was trying to do something like this but i don't know much about this language:

def teste (*array1, *array2)

Is this possible to do? I have to pass one array of numbers and strings and one only of strings but i dont know how much elements each one has.

2
  • It would be helpful if you pasted more of your code. Commented Nov 3, 2011 at 11:28
  • @Richard He wants 2 variable length argument lists automatically handled by the language. Something like teste(var1, var2, var3, var4) where var1 and var2 are part of array1 and var3 and var4 are part of array2. As I said in my answer, it's not possible. Commented Nov 3, 2011 at 11:30

4 Answers 4

2

It doesn't matter how many elements both of your array arguments have. For your purpose you can do just this:

def teste(arr_of_strings_and_numbers, arr_of_only_strings)

Now call the method like this:

teste( [1, 2, 3, 'foo', 'bar'], ['foo', 'bar', 'baz'] )

In your method body you have now access to: arr_of_strings_and_numbers which equals [1, 2, 3, 'foo', 'bar'] and arr_of_only_strings which equals ['foo', 'bar', 'baz']

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

Comments

1

You can pass as many arguments as you need to.

You may not want to "splat"them, though, that does something pretty specific.

Comments

1

That's not possible, there's no way for the language to know to which array an element belongs to. However you can simply pass 2 arrays.

Comments

1

You can't have two variadic lists of arguments in a method. If it were legal to define a method with two variadic lists of arguments like you want to, how would ruby know which argument should go in which array when you call say teste(a,b,c)? Should this result in array1 = [a]; array2 = [b,c] or array1 = [a,b]; array2=[c]? It's impossible to say.

If you want to pass two arrays to a method, just define it to take two regular arguments and then call it with two arrays.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.