user2012677
user2012677

Reputation: 5745

Reference YAML Node using method parameter

I load a yaml, and need to define a scope for it, to reference a specific node.

myYaml = YAML.load_file('myfile.yml').with_indifferent_access

Normally, I can just do

myYaml[:first_node][:first_child][:second_child]

However, I wanted to pass the path to a method to scope it for me. I am struggling to do something like this..

scope_path = [:first_node,:first_child,:second_child]

def scope(scope_path)
 myYAML[scope_path]
end

# So I need code to convert my scope_path parameter to

myYaml[:first_node][:first_child][:second_child]

Upvotes: 1

Views: 145

Answers (1)

spickermann
spickermann

Reputation: 106972

You can simple use Hash#dig:

myYaml.dig(:first_node, :first_child, :second_child)

Upvotes: 4

Related Questions