# Manipulating an Array of Structs

**URL:** <https://www.ruby-forum.com/t/manipulating-an-array-of-structs/186480>\
**Category:** Ruby\
**Created:** [April 7, 2010, 6:22pm UTC](https://www.ruby-forum.com/t/manipulating-an-array-of-structs/186480 "2010-04-07T18:22:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [April 7, 2010, 6:22pm UTC](https://www.ruby-forum.com/t/manipulating-an-array-of-structs/186480/1 "2010-04-07T18:22:57Z")

</div>

I have data that I’ve extracted from a database, that I wish to pass to  
a calling (external) piece of software through Drb.

The are a large number of data set formats, but an employee based  
example might look something like:

ID Name Age  
1 Mary Smith 23  
3 Frank Zappa 52  
19 Mary Jones 41

I’m trying to find a “nice” structure to pass this data around in, so  
that it can be easily understood and manipulated.

I’m currently using an Array of Hashes so it looks something like:

[ { :id =\> 1, :name =\> “Mary Smith”, :age =\> 23 }, { :id =\> 3, :name =\>  
“Frank Zappa”, :age =\> 52 }, { :id =\>19, :name =\> “Mary Jones”, :age =\>  
41 } ]

or more nicely formatted

[  
{ :id =\> 1, :name =\> “Mary Smith”, :age =\> 23 },  
{ :id =\> 3, :name =\> “Frank Zappa”, :age =\> 52 },  
{ :id =\>19, :name =\> “Mary Jones”, :age =\> 41 }  
]

This is fine if I want to extract single pieces of information, but it’s  
clumsy to manipulate the whole array, for example, extract out all the  
names, sort the array by age, create a new array containing employees  
over 40. It’s also inefficient as I’m repeating the labels on every  
record.

I realise I can do this by writing block code to process the array, but  
I feel there must be a cleaner way to store and manipulate data of this  
form in ruby. Something on the lines of Struct would be great to remove  
the excessing labels, but Struct doesn’t help with the array  
manipulation.

Suggestions would be really welcome, preferabling using standard  
classes, but will look at extensions.

thanks,

Anthony W…

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [April 7, 2010, 6:39pm UTC](https://www.ruby-forum.com/t/manipulating-an-array-of-structs/186480/2 "2010-04-07T18:39:21Z")

</div>

2010/4/7 Anthony W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> This is fine if I want to extract single pieces of information, but it’s  
> clumsy to manipulate the whole array, for example, extract out all the  
> names, sort the array by age, create a new array containing employees  
> over 40. It’s also inefficient as I’m repeating the labels on every record.
> 
> I realise I can do this by writing block code to process the array, but  
> I feel there must be a cleaner way to store and manipulate data of this  
> form in ruby. Something on the lines of Struct would be great to remove  
> the excessing labels, but Struct doesn’t help with the array manipulation.

I don’t see your issue with the “array manipulation”. Fact is, that  
if you have a collection of items you have to either iterate through  
all to process and filter them or you need to create indexes which you  
need to keep in sync with the base data (much the same way a RDBMS  
does btw.).

We have Enumerable#map, #select, #reject and a few more methods which  
work pretty well. If you use Struct or OpenStruct you can even use  
map like this:

all\_names = employees.map(&:name)

If find that succinct enough. And even with Hashes you get

all\_names = employees.map {|emp| emp[:name]}

This is a bit more verbose but still not too way off I’d say. My  
0.02EUR.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [April 7, 2010, 6:54pm UTC](https://www.ruby-forum.com/t/manipulating-an-array-of-structs/186480/3 "2010-04-07T18:54:35Z")

</div>

On Wed, Apr 7, 2010 at 6:39 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 19 Mary Jones 41
> 
> all\_names = employees.map(&:name)
> 
> If find that succinct enough. And even with Hashes you get
> 
> all\_names = employees.map {|emp| emp[:name]}
> 
> This is a bit more verbose but still not too way off I’d say. My 0.02EUR.

If you want to spare the client of that data to be performing the same  
kind of work, you can build a class that provides those methods. For  
example:

Employee = Struct.new(:id, :name, :age)

class EmployeeSet  
def initialize  
@employee\_list = []  
end  
def add emp  
@employee\_list \<\< emp  
end

def all\_names  
@employee\_list.map {|e| e.name}  
end

# and so on for all the method you would provide

end

list = EmployeeSet.new  
list.add Employee.new(1, “Mary”, 23)  
#…  
list.all\_names

Jesus.
