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

On 9/29/2010 2:45 PM, Paul R. 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

Jeremy B. wrote:

On 9/29/2010 2:45 PM, Paul R. 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?

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

age

an_arry.each { |item| puts “#{item.name} #{item.age}” }

-Jeremy

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs