# How to make "generic" YAML calls?

**URL:** <https://www.ruby-forum.com/t/how-to-make-generic-yaml-calls/77840>\
**Category:** Ruby\
**Created:** [December 1, 2006, 12:05am UTC](https://www.ruby-forum.com/t/how-to-make-generic-yaml-calls/77840 "2006-12-01T00:05:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 1, 2006, 12:05am UTC](https://www.ruby-forum.com/t/how-to-make-generic-yaml-calls/77840/1 "2006-12-01T00:05:49Z")

</div>

Hi,

I have a Test.Yaml file like;  
This:  
Thing: Foo  
That:  
Thing: Bar

My program works with this code;  
require ‘yaml’  
LocalTree = YAML.load\_file(“test.yaml”)  
puts LocalTree[“This”][“Thing”]  
puts LocalTree[“That”][“Thing”]

However, I want to write a class where I pass something like  
‘[“This”][“Thing”]’  
or  
‘[“That”][“Thing”]’  
and then make a call like;  
puts LocalTree@YamlLocation  
but I can’t figure out how to make this work.

Any hints/tips appreciated.

Thanks,  
Dale

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 1, 2006, 12:45am UTC](https://www.ruby-forum.com/t/how-to-make-generic-yaml-calls/77840/2 "2006-12-01T00:45:56Z")

</div>

removed\_email\_address@domain.invalid wrote:

> However, I want to write a class where I pass something like  
> ‘[“This”][“Thing”]’  
> or  
> ‘[“That”][“Thing”]’  
> and then make a call like;  
> puts LocalTree@YamlLocation  
> but I can’t figure out how to make this work.

Found it, sorry to bother.  
yaml\_vari = ‘[“This”][“Thing”]’  
puts eval(“LocalTree” + yaml\_vari)

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 1, 2006, 3:00pm UTC](https://www.ruby-forum.com/t/how-to-make-generic-yaml-calls/77840/3 "2006-12-01T15:00:02Z")

</div>

On Fri, Dec 01, 2006 at 08:45:05AM +0900, removed\_email\_address@domain.invalid  
wrote:  
} removed\_email\_address@domain.invalid wrote:  
}   
} \> However, I want to write a class where I pass something like  
} \> ‘[“This”][“Thing”]’  
} \> or  
} \> ‘[“That”][“Thing”]’  
} \> and then make a call like;  
} \> puts LocalTree@YamlLocation  
} \> but I can’t figure out how to make this work.  
}  
} Found it, sorry to bother.  
} yaml\_vari = ‘[“This”][“Thing”]’  
} puts eval(“LocalTree” + yaml\_vari)

In general, eval should be avoided. Try this:

def retrieve(\*path)  
path.inject(LocalTree) { |obj,key| obj[key] }  
end

Call it as retrieve(‘This’, ‘Thing’), for your example.

–Greg

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 4, 2006, 9:21pm UTC](https://www.ruby-forum.com/t/how-to-make-generic-yaml-calls/77840/4 "2006-12-04T21:21:02Z")

</div>

Gregory S. wrote:

> }  
> Call it as retrieve(‘This’, ‘Thing’), for your example.
> 
> –Greg

Thank you Greg.

I applied your suggestion to my application and it works.

In trying to further understand how .inject works, I looked in the  
pickaxe book and found that this would cause (possibly many)  
intermediate values to be retrieved (depending on YAML configuration)  
while only the last value is returned. I ran a quick benchmark and the  
.inject code was about 28% slower than using eval in my application.

This “slowdown” doesn’t really matter, but I was thinking that perhaps  
it could be significant if the YAML file size increased. But, if this  
happens, perhaps I shouldn’t be using YAML to store the data. 🙂

Do you know of a way to reduce the number of evaluations that occur  
when using inject?

Also, can you give a quick pointer as to why eval should be avoided?

Thanks again for your reply.  
Dale
