H
Howard Roberts
Suppose I have an array of two different kinds of objects, each with
different attributes, but both having a date attribute. I'd like to be
able to loop through the array in such a manner as to be able print a
result similar to the following:
07/19/08
Foo-1 - Name
attr 1
attr 2
[..]
Foo-2 - Name
attr 1
attr 2
[..]
Bar - Name
attr 1
attr 2
[..]
07/20/08
Foo-1 - Name
attr 1
attr 2
[..]
Bar - Name
attr 1
attr 2
[..]
etc.
showing all of the Foo's first, then the Bar's, for each date.
If I only have Foo's, I can easily do:
@data.sort{|x,y| Time.parse(x.date) <=> Time.parse(y.date)}).each do
|foo|
puts Foo.date # Still shows date on each loop, but okay
puts Foo.name
[..]
end
To get things in date order, but when I consider mixing a second object
into the array, that confounds matters for me. I suspect group_by{|item|
item.class} may be part of the solution, but I'm not sure how?
Any pointers would be most appreciated.
Thanks,
Howard
different attributes, but both having a date attribute. I'd like to be
able to loop through the array in such a manner as to be able print a
result similar to the following:
07/19/08
Foo-1 - Name
attr 1
attr 2
[..]
Foo-2 - Name
attr 1
attr 2
[..]
Bar - Name
attr 1
attr 2
[..]
07/20/08
Foo-1 - Name
attr 1
attr 2
[..]
Bar - Name
attr 1
attr 2
[..]
etc.
showing all of the Foo's first, then the Bar's, for each date.
If I only have Foo's, I can easily do:
@data.sort{|x,y| Time.parse(x.date) <=> Time.parse(y.date)}).each do
|foo|
puts Foo.date # Still shows date on each loop, but okay
puts Foo.name
[..]
end
To get things in date order, but when I consider mixing a second object
into the array, that confounds matters for me. I suspect group_by{|item|
item.class} may be part of the solution, but I'm not sure how?
Any pointers would be most appreciated.
Thanks,
Howard