[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to iterate of the attributes of an object?

Rich Sturim

9/26/2008 4:07:00 PM

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(",")
--
Posted via http://www.ruby-....

3 Answers

Li Chen

9/26/2008 5:14:00 PM

0

Rich Sturim 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

--
Posted via http://www.ruby-....

Sebastian Hungerecker

9/26/2008 6:07:00 PM

0

Rich Sturim 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
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Gaspard Bucher

9/26/2008 6:40:00 PM

0

If you are trying to serialize, deserialize an object, you might be
insterested in http://json.ruby....

g.

> 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(",")
> --
> Posted via http://www.ruby-....
>