How to copy arrays?

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

···

--
Posted via http://www.ruby-forum.com/.

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

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

···

On Nov 21, 2007 2:20 PM, Martin Durai <martin@angleritech.com> wrote:

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

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Wed, Nov 21, 2007 at 12:38:36PM +0900, Martin Durai wrote:

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

···

On Nov 21, 2007 2:38 PM, Martin Durai <martin@angleritech.com> wrote:

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
--
Posted via http://www.ruby-forum.com/\.

On Behalf Of 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.

i think you only want to copy a part (the whole is trivial) of the array and insert it to another array, no?

a
#=> [1, 2, 3, 4]

b
#=> [5, 6, 7, 8, 9, 10]

a[1,1]
#=> [2]

b[2..-2]
#=> [7, 8, 9]

a[1,1]=b[2..-2]
#=> [7, 8, 9]

a
#=> [1, 7, 8, 9, 3, 4]

or maybe just plain insert it,

a[0]
#=> 1

b[0,2]
#=> [5, 6]

a[0]=b[0,2]
#=> [5, 6]

a
#=> [[5, 6], 7, 8, 9, 3, 4]

kind regards -botp

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

···

On 21.11.2007 04:38, Martin Durai wrote:

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

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

···

On 21.11.2007 07:58, Robert Klemme wrote:

On 21.11.2007 04:38, Martin Durai wrote:

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.