Skip to content

Instantly share code, notes, and snippets.

@joey-g
Created May 7, 2018 13:05
Show Gist options
  • Save joey-g/a568a0df2e73df443bb8c61660baf5a6 to your computer and use it in GitHub Desktop.
Save joey-g/a568a0df2e73df443bb8c61660baf5a6 to your computer and use it in GitHub Desktop.
Update / Add hash to array
def get_default_features
[
{'name' => 'Feature1', 'enabled' => true},
{'name' => 'Feature2', 'enabled' => true}
]
end
# Param will be in the shape:
# {'name': 'FeatureName', 'enabled': true/false}
def set_feature(original_feature_set, feature_to_set)
# Determine if feature already exists in default set
matched_feature = original_feature_set.find { |f| f['name'] == feature_to_set['name'] }
if matched_feature
matched_feature['enabled'] = feature_to_set['enabled']
else
# No feature match was found, push new feature onto Array.
original_feature_set << feature_to_set
end
original_feature_set
end
disable_example =
{'name' => 'Feature1', 'enabled' => false}
new_feature_example =
{'name' => 'Feature3', 'enabled' => true}
default_features = get_default_features
final_features = set_feature(default_features, disable_example)
puts final_features
final_features_2 = set_feature(default_features, new_feature_example)
puts final_features_2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment