1

Can I pass values from one array to another array? Here is an example:

@a = [{:id => '1', :name => 'abcd'}, {:id => '2', :name => "asadad"}, ...]
@b = [0, 1]

I want to pass values from @a to @b like this:

@b[0] = {:id => '1', :name => 'abcd'}
...
@b[1] = {:id => '2', :name => 'asadad'}

Can I do that and how can I do that?

2
  • Does [0,1] refer to indices in @a? Commented Feb 6, 2015 at 10:12
  • You want to move all values from @a to @b or just some specific ones? Commented Feb 6, 2015 at 10:12

1 Answer 1

3

Not clear what you are doing, but it looks like this:

@b = @a.values_at(*@b)
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please explain what does the * do. Does it have any relation with the pointers or so.
Yeah!!That's what I want to do, thanks you so much :)
@Deep it expands the array elements to an argument list. values_at(*[1,2,3]) becomes values_at(1,2,3)

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.