Reputation: 41
Is there any ruby method that I can use to replace two different arrays elements? For instance, I have these two arrays:
#Before exchange
arr_one = [1,2,3,4,5]
arr_two = ["some", "thing", "new"]
After replacements the elements I am expecting something like this:
#After exchange
arr_one = ["some", "thing", "new"]
arr_two = [1,2,3,4,5]
How can I handle this with or without a ruby method?
Upvotes: 1
Views: 48
Reputation: 198324
If you need to have the arrays stay put (i.e. the values of the variables not change) and exchange the contents, then this should do:
arr_tmp = arr_one.dup
arr_one.replace(arr_two)
arr_two.replace(arr_tmp)
Upvotes: 4
Reputation: 51151
You mean, you want to 'exchange' values of local variables? It's quite easy in Ruby:
arr_one, arr_two = arr_two, arr_one
Upvotes: 5