How can I copy an array and its elements? (call by value)

Hi all,

I'm failing at something really basic: copying an array and its
contents, so that I can modify the copy without modifying the original.

Here's an example:

irb(main):006:0> a = [1, 2]
=> [1, 2]
irb(main):007:0> b = a
=> [1, 2]
irb(main):008:0> b.delete(1)
=> 1
irb(main):009:0> b
=> [2]
irb(main):010:0> a
=> [2]

I guess the problem is that arrays are called by name not by value, so
that 'b' copy points to the same adress in memory as 'a'. How can I
create an independent copy of 'a'?

Thanks,
Janus

···

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

Hello,

I'm failing at something really basic: copying an array and its
contents, so that I can modify the copy without modifying the original.

I think the method 'dup' is what you are looking for

irb(main):001:0> a = [1,2]
=> [1, 2]
irb(main):002:0> b = a.dup
=> [1, 2]
irb(main):003:0> b.delete(1)
=> 1
irb(main):004:0> b
=> [2]
irb(main):005:0> a
=> [1, 2]

Cheers,

···

--
JJ Fleck
PCSI1 Lycée Kleber

b=a[0..-1]

···

Le Fri, 7 Aug 2009 10:18:52 -0500, Janus Bor <janus@urban-youth.com> a écrit :

How can I create an independent copy of 'a'?

--
Arnaud

Discount Ed hardy tshirt (www.ebuyings.com)
Discount Ed hardy jean (www.ebuyings.com)
Discount Ed hardy shoes (www.ebuyings.com)
Discount Ed hardy handbag (www.ebuyings.com)
Discount Ed hardy other porduct (www.ebuyings.com)
Discount Nike air jordans (www.ebuyings.com)
Discount Nike Air Max 90 Sneakers (www.ebuyings.com)
Discount Nike Air Max 91 Supplier (www.ebuyings.com)
Discount Nike Air Max 95 Shoes Supplier (www.ebuyings.com)
Discount Nike Air Max 97 Trainers (www.ebuyings.com)
Discount Nike Air Max 2003 Wholesale (www.ebuyings.com)
Discount Nike Air Max 2004 Shoes Wholesale
(www.ebuyings.com)
Discount Nike Air Max 2005 Shop (www.ebuyings.com)
Discount Nike Air Max 2006 Shoes Shop (www.ebuyings.com)
Discount Nike Air Max 360 Catalogs (www.ebuyings.com)
Discount Nike Air Max Ltd Shoes Catalogs (www.ebuyings.com)
Discount Nike Air Max Tn Men's Shoes (www.ebuyings.com)
Discount Nike Air Max Tn 2 Women's Shoes (www.ebuyings.com)
Discount Nike Air Max Tn 3 Customize (www.ebuyings.com)
Discount Nike Air Max Tn 4 Shoes Customize
( www.ebuyings.com)
Discount Nike Air Max Tn 6 Supply (www.ebuyings.com)
Discount Nike Shox NZ Shoes Supply (www.ebuyings.com)
Discount Nike Shox OZ Sale (www.ebuyings.com)
Discount Nike Shox TL Store (www.ebuyings.com)
Discount Nike Shox TL 2 Shoes Store (www.ebuyings.com)
Discount Nike Shox TL 3 Distributor (www.ebuyings.com)
Discount Nike Shox Bmw Shoes Distributor (www.ebuyings.com)
Discount Nike Shox Elite Shoes Manufacturer
(www.ebuyings.com)
Discount Nike Shox Monster Manufacturer (www.ebuyings.com)
Discount Nike Shox R4 Running Shoes (www.ebuyings.com)
Discount Nike Shox R5 Mens Shoes (www.ebuyings.com)
Discount Nike Shox Ride Womens Shoes (www.ebuyings.com)
Discount Nike Shox Rival Shoes Wholesaler (www.ebuyings.com)
Discount Nike Shox Energia Wholesaler (www.ebuyings.com)
Discount Nike Shox LV Sneaker (www.ebuyings.com)
Discount Nike Shox Turbo Suppliers (www.ebuyings.com)
Discount Nike Shox Classic Shoes Suppliers
(www.ebuyings.com)
Discount Nike Shox Dendara Trainer (www.ebuyings.com)
Discount Nike Air Jordan 1 Seller (www.ebuyings.com)
Discount Nike Air Jordan 2 Shoes Seller (www.ebuyings.com)
Discount Nike Air Jordan 3 Collection (www.ebuyings.com)
Discount Nike Air Jordan 4 Shoes Collection
(www.ebuyings.com)
Discount Nike Air Jordan 5 Chaussure Shoes
(www.ebuyings.com)
Discount Nike Air Jordan 6 Catalog (www.ebuyings.com)
Discount Nike Air Jordan 7 Shoes Catalog (www.ebuyings.com)
Discount Nike Air Jordan 8 Customized (www.ebuyings.com)
Discount Nike Air Jordan 9 Shoes Customized
(www.ebuyings.com)
Discount Nike Air Jordan 10 Wholesalers (www.ebuyings.com)
Discount Nike Jordan 11 Shoes Wholesalers (www.ebuyings.com)
Discount Nike Air Jordan 12 Factory (www.ebuyings.com)
Discount Nike Air Jordan 13 Shoes Factory (www.ebuyings.com)
Discount Nike Air Jordan 14 Shoes Sell (www.ebuyings.com)
Discount Nike Air Jordan 16 Exporter (www.ebuyings.com)
Discount Nike Air Jordan 17 Shoes Exporter
(www.ebuyings.com)
Discount Nike Air Jordan 18 Offer (www.ebuyings.com)
Discount Nike Air Jordan 19 Shoes Offer (www.ebuyings.com)
Discount Nike Air Jordan 20 Manufacture (www.ebuyings.com)
Discount Nike Jordan 21 Shoes Manufacture (www.ebuyings.com)

Thank you both, that's exactly what I'm looking for!

···

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

Just note that #dup and #clone do a shallow copy, so if you modify objects in the copy this change will also be seen via the original Array. Depending on what you do #map might be more appropriate.

Kind regards

  robert

···

On 07.08.2009 17:47, Janus Bor wrote:

Thank you both, that's exactly what I'm looking for!

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/