tags:

views:

826

answers:

2

This code works, of course:

@x = { :all => { :x => 1, :y => 2 } }

But this doesn't:

@x = { :abc, :all => { :x => 1, :y => 2 } }

Is there any way to do what I want here? i.e. I want two keys in a hash to each refer to the same (copy of a) value. But I only want to specify the value once.

+5  A: 

@x = { :all => tmp = { :x => 1, :y => 2 }, :abc => tmp }

Tobias
That's not a copy though, but you can fix that by using tmp.dup (shallow copy)
sris
True. I interpreted "the same copy of a value" as meaning one instance.
Tobias
A: 

How about this:

@x = { :all => tmp = { :x => 1, :y => 2 }, :abc => tmp.reject {|k,v| false} }

Gautam Rege