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
On Thu, Apr 10, 2008 at 6:45 AM, Mario R. [email protected] wrote:
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
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
On Thu, Apr 10, 2008 at 6:57 AM, Mario R. [email protected] wrote:
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
Hi –
On Thu, 10 Apr 2008, Mario R. wrote:
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
Ok.
I was thinking that maybe it could be an easier way…
Thank you.