# How to iterate of the attributes of an object?

**URL:** <https://www.ruby-forum.com/t/how-to-iterate-of-the-attributes-of-an-object/149601>\
**Category:** Ruby\
**Created:** [September 26, 2008, 6:06pm UTC](https://www.ruby-forum.com/t/how-to-iterate-of-the-attributes-of-an-object/149601 "2008-09-26T18:06:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hotspoons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e68b1a/32.png) [@hotspoons](https://www.ruby-forum.com/u/hotspoons)\
**Post date:** [September 26, 2008, 6:06pm UTC](https://www.ruby-forum.com/t/how-to-iterate-of-the-attributes-of-an-object/149601/1 "2008-09-26T18:06:58Z")

</div>

I need to build a string of all populated attributes of a given object.

I need to get the attribute name and it’s value

Also,the object may have 10 attributes, but I’m only interested in the  
attribute that are not nil.

here’s a sample object:  
foo = Foo.new(:bar =\> ‘baz’, :bar2 =\> ‘baz2’, :bar3 =\> ‘baz3’)

ultimately what I want is this as a string like this:  
“:bar =\> ‘baz’, :bar2 =\> ‘baz2’, :bar3 =\> ‘baz3’”

code should be something like this? (sorry I’m a newbie)

s = “”

foo.instance\_values.each do |v|  
s += “:#{v.name} =\> #{v.value}”  
end

s.split(",")

---

<div class="post-metadata">

**Author:** ![hotspoons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e68b1a/32.png) [@hotspoons](https://www.ruby-forum.com/u/hotspoons)\
**Post date:** [September 26, 2008, 7:14pm UTC](https://www.ruby-forum.com/t/how-to-iterate-of-the-attributes-of-an-object/149601/2 "2008-09-26T19:14:16Z")

</div>

Rich S. wrote:

> I need to build a string of all populated attributes of a given object.
> 
> I need to get the attribute name and it’s value
> 
> Also,the object may have 10 attributes, but I’m only interested in the  
> attribute that are not nil.
> 
> here’s a sample object:  
> foo = Foo.new(:bar =\> ‘baz’, :bar2 =\> ‘baz2’, :bar3 =\> ‘baz3’)
> 
> ultimately what I want is this as a string like this:  
> “:bar =\> ‘baz’, :bar2 =\> ‘baz2’, :bar3 =\> ‘baz3’”
> 
> code should be something like this? (sorry I’m a newbie)
> 
> s = “”
> 
> foo.instance\_values.each do |v|  
> s += “:#{v.name} =\> #{v.value}”  
> end
> 
> s.split(",")

C:\Documents and Settings\chen73\>irb  
irb(main):001:0\> foo = {:bar =\> ‘baz’, :bar2 =\> ‘baz2’, :bar3 =\> ‘baz3’}  
=\> {:bar3=\>“baz3”, :bar=\>“baz”, :bar2=\>“baz2”}  
irb(main):002:0\> foo.each{|k,v| puts “#{k} “+”=\>”+"#{v}" }  
bar3 =\>baz3  
bar =\>baz  
bar2 =\>baz2  
=\> {:bar3=\>“baz3”, :bar=\>“baz”, :bar2=\>“baz2”}

Li

---

<div class="post-metadata">

**Author:** ![hotspoons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e68b1a/32.png) [@hotspoons](https://www.ruby-forum.com/u/hotspoons)\
**Post date:** [September 26, 2008, 8:08pm UTC](https://www.ruby-forum.com/t/how-to-iterate-of-the-attributes-of-an-object/149601/3 "2008-09-26T20:08:41Z")

</div>

Rich S. wrote:

> I need to build a string of all populated attributes of a given object.
> 
> I need to get the attribute name and it’s value

Define “attribute”. You can print a list of instance variables and their  
values like this:  
puts instance\_variables.map {|var| “#{var}:  
#{instance\_variable\_get(var)}”}

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![hotspoons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e68b1a/32.png) [@hotspoons](https://www.ruby-forum.com/u/hotspoons)\
**Post date:** [September 26, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/how-to-iterate-of-the-attributes-of-an-object/149601/4 "2008-09-26T20:40:25Z")

</div>

If you are trying to serialize, deserialize an object, you might be  
insterested in [http://json.rubyforge.org/](http://json.rubyforge.org/).

g.
