Problems with Structs

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:inload’: uninitialized constant
TestStruct at Struct (NameError)

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

Any ideas?

Regards,

Dan

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) wrote:

and then, in the sample client I do this:

a = Marshal.load(v)