# Problems with Structs

**URL:** <https://rubytalk.org/t/problems-with-structs/1203>\
**Category:** ruby-talk\
**Created:** [14 August 2002 18:03 UTC](https://rubytalk.org/t/problems-with-structs/1203 "2002-08-14T18:03:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Berger6](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@Daniel\_Berger6](https://rubytalk.org/u/Daniel_Berger6)\
**Post date:** [14 August 2002 18:03 UTC](https://rubytalk.org/t/problems-with-structs/1203/1 "2002-08-14T18:03:17Z")

</div>

Hi all,

Ruby 1.6.7 on Solaris 9

This question refers back to the client/server program I posted in the  
"Problems with Marshal" post.

In the sample server from that post (using read/write instead of  
gets/puts this time), if I do this:

s = Struct.new(“TestStruct”,:name,:age)  
s1 = s.new(“Dan”,29)  
s2 = s.new(“Mary”,33)

a = []  
a.push(s1,s2)

x = Marshal.dump(a)

and then, in the sample client I do this:

a = Marshal.load(v)

a.each do |s|  
puts s[:name]  
puts s[:age]  
end

I get this:

(eval):17:in `load': (eval):17:in`load’: uninitialized constant  
TestStruct at Struct (NameError)

-line 17 is the ‘load(v)’ line.

Any ideas?

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![why\_the\_lucky\_stiff1](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@why\_the\_lucky\_stiff1](https://rubytalk.org/u/why_the_lucky_stiff1)\
**Post date:** [14 August 2002 18:55 UTC](https://rubytalk.org/t/problems-with-structs/1203/2 "2002-08-14T18:55:51Z")

</div>

You’ll need to define the TestStruct in the client as well, possibly  
by transmitting the Struct definition. This call:

s = Struct.new(“TestStruct”,:name,:age)

Is actually defining the Struct::TestStruct class in the server,  
as well as storing the constant name for the class in ‘s’. You’ll  
need a similiar call in the client.

If you want to transmit the Struct schema as well, you can use s.members  
to get an array of the Struct member names, which you can Marshal to the  
client.

Hope that makes sense,

\_why

> **···**
>
> Daniel Berger ([djberge@v55host11.interprise.com](mailto:djberge@v55host11.interprise.com)) wrote:
> 
> > and then, in the sample client I do this:
> > 
> > a = Marshal.load(v)
