Accessing variables of instances in an array?

Hi. I want to have an array of instances.......

listb = [instance1, instance2, instance3]

I am able to print out the instance.....

def printout(an_array)
an_array.each {|item| p item }
end
printout(listb)

How can I print out specific variables for each instance?

eg variables...

name
age

···

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

an_arry.each { |item| puts "#{item.name} #{item.age}" }

-Jeremy

···

On 9/29/2010 2:45 PM, Paul Roche wrote:

Hi. I want to have an array of instances.......

listb = [instance1, instance2, instance3]

I am able to print out the instance.....

def printout(an_array)
an_array.each {|item| p item }
end
printout(listb)

How can I print out specific variables for each instance?

eg variables...

name
age

Jeremy Bopp wrote:

···

On 9/29/2010 2:45 PM, Paul Roche wrote:

How can I print out specific variables for each instance?

eg variables...

name
age

an_arry.each { |item| puts "#{item.name} #{item.age}" }

-Jeremy

Thanks Jeremy, that does the job. By the way is 'item' a keyword?
--
Posted via http://www.ruby-forum.com/\.

No, item is not a keyword. You can replace that with any valid variable
name. When you start using other methods that take blocks, you'll find
that you can use more than one variable within the | ... | as well.
Some blocks even take an empty list.

-Jeremy

···

On 9/29/2010 3:15 PM, Paul Roche wrote:

Jeremy Bopp wrote:

On 9/29/2010 2:45 PM, Paul Roche wrote:

How can I print out specific variables for each instance?

eg variables...

name
age

an_arry.each { |item| puts "#{item.name} #{item.age}" }

-Jeremy

Thanks Jeremy, that does the job. By the way is 'item' a keyword?