You can avoid the duplicate objects by using bang methods,
Don’ts
hash = { one: "one", two: "two"} puts hash.merge(three: "three" ) - > {:one=>"one", :two=>"two", :three=>"three"} puts hash -> {:one=>"one", :two=>"two"} # here you have to assign merged value in a new variable new_hash = hash.merge(three: "three" ) puts hash -> {:one=>"one", :two=>"two", :three=>"three"}
Do’s
hash = { one: "one", two: "two", three: "three" } puts hash.merge!(three: "three" ) - > {:one=>"one", :two=>"two", :three=>"three"} puts hash -> {:one=>"one", :two=>"two", :three=>"three"}
Leave a Reply