Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > global array of hashes

Reply
Thread Tools

global array of hashes

 
 
Mario Ruiz
Guest
Posts: n/a
 
      04-10-2008
I'm doing this:

$globvar=Array.new()
$globvar[0]="mygenericvalue"
localvar=$globvar
localvar[0]="mylocalvalue"
puts $globvar

And the result for $globvar is "mylocalvalue"

How can I assign to localvar only the value of $globvar and not the
memory possition??

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

 
Reply With Quote
 
 
 
 
Todd Benson
Guest
Posts: n/a
 
      04-10-2008
On Thu, Apr 10, 2008 at 6:45 AM, Mario Ruiz <> wrote:
> I'm doing this:
>
> $globvar=Array.new()
> $globvar[0]="mygenericvalue"
> localvar=$globvar
> localvar[0]="mylocalvalue"
> puts $globvar
>
> And the result for $globvar is "mylocalvalue"
>
> How can I assign to localvar only the value of $globvar and not the
> memory possition??
>
> Thanks


This seems to work...

localvar = $globvar.dup

Todd

 
Reply With Quote
 
 
 
 
Mario Ruiz
Guest
Posts: n/a
 
      04-10-2008
But not in this case:

$globvar=Array.new()
myHash=Hash.new()
myHash["oneValue"]="mygenericvalue"
$globvar[0]=myHash

localvar=$globvar.dup

localvar[0]["oneValue"]="mylocalvalue"

puts $globvar
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      04-10-2008
Hi --

On Thu, 10 Apr 2008, Mario Ruiz wrote:

> But not in this case:
>
> $globvar=Array.new()
> myHash=Hash.new()
> myHash["oneValue"]="mygenericvalue"
> $globvar[0]=myHash
>
> localvar=$globvar.dup
>
> localvar[0]["oneValue"]="mylocalvalue"
>
> puts $globvar


Try this:

irb(main):001:0> a = []
=> []
irb(main):002:0> h = {}
=> {}
irb(main):003:0> h["one"] = "generic"
=> "generic"
irb(main):004:0> a[0] = h
=> {"one"=>"generic"}
irb(main):005:0> l = Marshal.load(Marshal.dump(a))
=> [{"one"=>"generic"}]
irb(main):006:0> l[0]["one"] = "local"
=> "local"
irb(main):007:0> a
=> [{"one"=>"generic"}]

(using globals if you absolutely must... but that's another story


David

--
Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS April 14-17 New York City
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.rubypal.com for details and updates!

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      04-10-2008
On Thu, Apr 10, 2008 at 6:57 AM, Mario Ruiz <> wrote:
> But not in this case:
>
> $globvar=Array.new()
> myHash=Hash.new()
> myHash["oneValue"]="mygenericvalue"
> $globvar[0]=myHash
>
> localvar=$globvar.dup
>
> localvar[0]["oneValue"]="mylocalvalue"
>
> puts $globvar


In this case, you create a new array that holds the same Hash object.
You have to dup the Hash as well.

hth,
Todd

 
Reply With Quote
 
Mario Ruiz
Guest
Posts: n/a
 
      04-10-2008
Ok.

I was thinking that maybe it could be an easier way...

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Array of Hashes in an array of hashes - Complicated! Matt Brooks Ruby 16 09-16-2009 05:53 PM
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
using hashes as keys in hashes Steven Arnold Ruby 3 11-23-2005 03:25 PM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57