# How to loop through element after an inspect?

**URL:** <https://www.ruby-forum.com/t/how-to-loop-through-element-after-an-inspect/148885>\
**Category:** Rails\
**Created:** [September 18, 2008, 12:05am UTC](https://www.ruby-forum.com/t/how-to-loop-through-element-after-an-inspect/148885 "2008-09-18T00:05:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdunneuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jdunneuk](https://www.ruby-forum.com/u/jdunneuk)\
**Post date:** [September 18, 2008, 12:05am UTC](https://www.ruby-forum.com/t/how-to-loop-through-element-after-an-inspect/148885/1 "2008-09-18T00:05:48Z")

</div>

`records = YAML.load_file("myfile.yml")
records.each do |record|
  puts record.inspect
end`

I want to be able to read an arbitrary yml file, then print out each of  
the element one by one.

In the above example, my output is  
#\<RmEnv label\_label\_id: 8, data\_id: 27, data\_val: “MJP-PR2”\>  
#\<RmEnv label\_label\_id: 8, data\_id: 28, data\_val: “MJP-QA2”\>

I know I can do  
puts record.label\_label\_id  
puts record.data\_id  
puts record.data\_val

but that only works when reading myfile.yml. If reading other yml, it’ll  
be a different object.

I was hoping something like this would work

`
record.each do |key|
  puts record[key]
end`

But RoR throw an error, undefined method ‘each’ for #\<RmEnv … \>

---

<div class="post-metadata">

**Author:** ![jdunneuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jdunneuk](https://www.ruby-forum.com/u/jdunneuk)\
**Post date:** [September 18, 2008, 9:05am UTC](https://www.ruby-forum.com/t/how-to-loop-through-element-after-an-inspect/148885/2 "2008-09-18T09:05:30Z")

</div>

On Sep 17, 11:05 pm, John Do [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> #\<RmEnv label\_label\_id: 8, data\_id: 28, data\_val: “MJP-QA2”\>
> 
> `
> record.each do |key|
> puts record[key]
> end`
> 
> But RoR throw an error, undefined method ‘each’ for #\<RmEnv … \>

In the most general case there is not much you can do. While you can  
list all methods on an object, you can’t tell ahead of time whether  
these are just accessors or methods that do stuff. Methods might also  
be added only when needed (eg activerecord objects).  
You can dump the instance variables from the object, but it may not  
always be obvious which ones are important and which ones not.  
If you can make the assumption that all your object share some  
property (eg all descend from some base class etc…) then your life  
will be easier

Fred (PS this is a pure ruby thing - no rails magic happening here)

---

<div class="post-metadata">

**Author:** ![jdunneuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jdunneuk](https://www.ruby-forum.com/u/jdunneuk)\
**Post date:** [September 18, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/how-to-loop-through-element-after-an-inspect/148885/3 "2008-09-18T19:19:33Z")

</div>

Frederick C. wrote:

> On Sep 17, 11:05ï¿½pm, John Do [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > #\<RmEnv label\_label\_id: 8, data\_id: 28, data\_val: “MJP-QA2”\>
> > 
> > `
> > record.each do |key|
> > ï¿½ puts record[key]
> > end`
> > 
> > But RoR throw an error, undefined method ‘each’ for #\<RmEnv … \>
> 
> In the most general case there is not much you can do. While you can  
> list all methods on an object, you can’t tell ahead of time whether  
> these are just accessors or methods that do stuff. Methods might also  
> be added only when needed (eg activerecord objects).  
> You can dump the instance variables from the object, but it may not  
> always be obvious which ones are important and which ones not.  
> If you can make the assumption that all your object share some  
> property (eg all descend from some base class etc…) then your life  
> will be easier
> 
> Fred (PS this is a pure ruby thing - no rails magic happening here)

The #\<blah key1: value1, key2: value2 …\> comes from YAML. There’s no  
way to pull out all the keys?

I know if the yaml file doesn’t contains - !ruby/object:RmEnv, YAML.load  
will create a hash #\<key1: value1, key2: value2 …\> which I can loop  
through with each\_key.

If it’s a ruby object, I’m out of luck?

The data is just a database dump of a Rail Model RmEnv where keys are  
the db field and the values their values.

---

<div class="post-metadata">

**Author:** ![jdunneuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jdunneuk](https://www.ruby-forum.com/u/jdunneuk)\
**Post date:** [September 18, 2008, 8:19pm UTC](https://www.ruby-forum.com/t/how-to-loop-through-element-after-an-inspect/148885/4 "2008-09-18T20:19:06Z")

</div>

Frederick C. wrote:

> On Sep 18, 6:19ï¿½pm, John Do [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > The #\<blah key1: value1, key2: value2 …\> comes from YAML. There’s no  
> > way to pull out all the keys?
> 
> Well you can certainly just dump instance variables from the object  
> (which is probably what yaml does most of the time)

How do you dump the instance variables from an object? Would you know of  
an online reference for what’s avaialable for object manipulation?

---

<div class="post-metadata">

**Author:** ![jdunneuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jdunneuk](https://www.ruby-forum.com/u/jdunneuk)\
**Post date:** [September 18, 2008, 8:14pm UTC](https://www.ruby-forum.com/t/how-to-loop-through-element-after-an-inspect/148885/5 "2008-09-18T20:14:56Z")

</div>

On Sep 18, 6:19 pm, John Do [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The #\<blah key1: value1, key2: value2 …\> comes from YAML. There’s no  
> way to pull out all the keys?

Well you can certainly just dump instance variables from the object  
(which is probably what yaml does most of the time)

> I know if the yaml file doesn’t contains - !ruby/object:RmEnv, YAML.load  
> will create a hash #\<key1: value1, key2: value2 …\> which I can loop  
> through with each\_key.
> 
> If it’s a ruby object, I’m out of luck?

Not so much out of look, most objects just don’t have a way for  
iterating over all their properties (for whatever definition of  
properties they have)

> The data is just a database dump of a Rail Model RmEnv where keys are  
> the db field and the values their values.

You might be better off dumping the attributes of your activerecord  
objects rather then the whole thing.

Fred
