Rao Ehtesham
Rao Ehtesham

Reputation: 11

Global variable gets changed , if we assign it to local variable and changing local variable

$permission = YAML.load_file('permission.yml').deep_symbolize_keys

local_permission = $permission['packing'.to_sym] 

local_permission['create'] = ['so1']

I am changing create of local_permission variable, but it gets changed into $permission. I don't want this. Please help me to solve this.

I am expecting that $permission['create'] should remain same even after changing local_permission['create'].

Upvotes: 1

Views: 88

Answers (1)

Felipe Zavan
Felipe Zavan

Reputation: 1813

You need to duplicate the original object, right now you're only assigning a reference to it.

You can use:

local_permission = $permission[:packing].clone
local_permission[:create] = ['so1']

# or if you're using rails and want everything to be cloned:

local_permission = $permission[:packing].deep_dup
local_permission[:create] = ['so1']

Upvotes: 2

Related Questions