How to copy arrays?

M

Martin Durai

I came accross another problem, how to copy arrays? Actually i need to
copy array just like in java. i will include the source code of java.
The same functionality i need in ruby. please any body help me with this

System.arraycopy(elName, 0, arr, 0, elStackSize);

I need a way to do samething in ruby. please any body help me with code
for arraycopy.

thanks in advance
 
D

Daniel N

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

I came accross another problem, how to copy arrays? Actually i need to
copy array just like in java. i will include the source code of java.
The same functionality i need in ruby. please any body help me with this

System.arraycopy(elName, 0, arr, 0, elStackSize);

I need a way to do samething in ruby. please any body help me with code
for arraycopy.

thanks in advance

The java doesn't mean anything to me :) That make me very happy.

@array.dup is the way to copy any object in ruby
 
M

Martin Durai

Thank you daniel, but my functionality is not return as a array.
I need to copy a source array to a destination array.

Thank you in advance
 
G

Gregory Seidman

Thank you daniel, but my functionality is not return as a array.
I need to copy a source array to a destination array.

def java_inspired_method_sans_idiomatic_ruby(destination_array)
destination_array.replace @my_array
end
Thank you in advance
--Greg
 
D

Daniel N

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Thank you daniel, but my functionality is not return as a array.
I need to copy a source array to a destination array.

Thank you in advance
I don't understand what your asking then. This is a fairly standard way to
copy an object.
@array = [1,2,3,4,5]

@new_array = @array.dup
 
P

Peña, Botp

T24gQmVoYWxmIE9mIE1hcnRpbiBEdXJhaToNCiMgVGhhbmsgeW91IGRhbmllbCwgYnV0IG15IGZ1
bmN0aW9uYWxpdHkgaXMgbm90IHJldHVybiBhcyBhIGFycmF5Lg0KIyBJIG5lZWQgdG8gY29weSBh
IHNvdXJjZSBhcnJheSB0byBhIGRlc3RpbmF0aW9uIGFycmF5Lg0KDQppIHRoaW5rIHlvdSBvbmx5
IHdhbnQgdG8gY29weSBhIHBhcnQgKHRoZSB3aG9sZSBpcyB0cml2aWFsKSBvZiB0aGUgYXJyYXkg
YW5kIGluc2VydCBpdCB0byBhbm90aGVyIGFycmF5LCBubz8NCg0KYQ0KIz0+IFsxLCAyLCAzLCA0
XQ0KDQpiDQojPT4gWzUsIDYsIDcsIDgsIDksIDEwXQ0KDQphWzEsMV0NCiM9PiBbMl0NCg0KYlsy
Li4tMl0NCiM9PiBbNywgOCwgOV0NCg0KYVsxLDFdPWJbMi4uLTJdDQojPT4gWzcsIDgsIDldDQoN
CmENCiM9PiBbMSwgNywgOCwgOSwgMywgNF0NCg0KDQpvciBtYXliZSBqdXN0IHBsYWluIGluc2Vy
dCBpdCwNCg0KYVswXQ0KIz0+IDENCg0KYlswLDJdDQojPT4gWzUsIDZdDQoNCmFbMF09YlswLDJd
DQojPT4gWzUsIDZdDQoNCmENCiM9PiBbWzUsIDZdLCA3LCA4LCA5LCAzLCA0XQ0KDQoNCmtpbmQg
cmVnYXJkcyAtYm90cA0K
 
R

Robert Klemme

Thank you daniel, but my functionality is not return as a array.
I need to copy a source array to a destination array.

Maybe before we go off guessing you can show a bit more of your code.
While there are methods to copy and insert into another array, maybe
there is a more efficient solution.

Cheers

robert
 
R

Robert Klemme

Maybe before we go off guessing you can show a bit more of your code.
While there are methods to copy and insert into another array, maybe
there is a more efficient solution.

I meant to include this

irb(main):007:0> a=(1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb(main):008:0> a[2,4]=%w{a b c}
=> ["a", "b", "c"]
irb(main):009:0> a
=> [1, 2, "a", "b", "c", 7, 8, 9, 10]
irb(main):010:0> a.concat %w{foo bar}
=> [1, 2, "a", "b", "c", 7, 8, 9, 10, "foo", "bar"]
irb(main):011:0> a
=> [1, 2, "a", "b", "c", 7, 8, 9, 10, "foo", "bar"]
irb(main):012:0>

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,353
Members
48,038
Latest member
HunterDela

Latest Threads

Top