Initialize Ruby hash with a constant value

819 Views Asked by At

Actually I want to use such constructs in chef attributes, where I initialize a structure with a constant and modify it

init_value = { "a" => { "b" => "c" } }
prepare = init_value
prepare["a"]["x"] = "y"

now init_value also contains ["a"]["x"] = "y", so when I prepare a new value

prepare = init_value
prepare["a"]["y"] = "x"

so prepare["a"] contains the keys ["b", "x", "y"].

How can I initialize prepare with a constant without quoting the constant, so that in the last step, prepare["a"] only contains the two keys ["b","y"]?

3

There are 3 best solutions below

2
On BEST ANSWER

You could move the initial hash into a method. This way, the method always returns a "fresh" hash:

def init_value
  {"a"=>{"b"=>"c"}}
end

prepare = init_value
prepare["a"]["x"] = "y"
prepare
#=> {"a"=>{"b"=>"c", "x"=>"y"}}

prepare = init_value
prepare["a"]["y"] = "x"
prepare
#=> {"a"=>{"b"=>"c", "y"=>"x"}}
1
On

I think you want a "deep copy" of init_value when assigning to prepare.

see: How do I copy a hash in Ruby?

2
On

Extracted from Rails 4.2.7

Implementation:

class Object
  def duplicable?
    true
  end
  def deep_dup
    duplicable? ? dup : self
  end
end 

class Hash
   def deep_dup
     each_with_object(dup) do |(key, value), hash|
       hash[key.deep_dup] = value.deep_dup
     end
  end
end

class Array
  def deep_dup
    map { |it| it.deep_dup }
  end
end

# Not duplicable? 
# if ruby version < 2.0 also add Class and Module as they were not duplicable until 2.0
[Method, Symbol, FalseClass, TrueClass, NilClass, Numeric, BigDecimal].each do |m|
  m.send(:define_method, :duplicable?, ->{false})
end

Then you could use a method for init_value so that deep_dup is always called and you can't accidentally forget

#since you asked for a constant
INIT_VALUE = { "a" => { "b" => "c" } }.freeze

def init_value 
  INIT_VALUE.deep_dup 
end

And usage as such

prepare = init_value
prepare["a"]["x"] = "y"

prepare2 = init_value
prepare2["a"]["y"] = "x"

prepare
#=> {"a"=>{"b"=>"c", "x"=>"y"}}
prepare2
#=> {"a"=>{"b"=>"c", "y"=>"x"}}