I just want to write a Ruby script (without rails) with a class that contains an array of identifiers. Here is my original class:
# define a "Person" class to represent the three expected columns class Person < # a Person has a first name, last name, and city Struct.new(:first_name, :last_name, :city) # a method to print out a csv record for the current Person. # note that you can easily re-arrange columns here, if desired. # also note that this method compensates for blank fields. def print_csv_record last_name.length==0 ? printf(",") : printf("\"%s\",", last_name) first_name.length==0 ? printf(",") : printf("\"%s\",", first_name) city.length==0 ? printf("") : printf("\"%s\"", city) printf("\n") end end
Now I would like to add an array called ids to the class, can I include it in a Struct.new statement such as Struct.new (: first_name ,: last_name ,: city ,: ids = Array.new) or create an instance array variable or any definition of individual methods or something else?
I would like to be able to do things like:
p = Person.new p.last_name = "Jim" p.first_name = "Plucket" p.city = "San Diego"
and iterate over the array
p.ids.each do |i| puts i end
source share