I don't think you can access the attributes of a Struct from inside it
using object variables (like @a).
C:\Documents and Settings\flifson\Desktop>irb
irb(main):001:0> X =3D Struct.new :a,:b
=3D> X
irb(main):002:0> class X
irb(main):003:1> def print_attributes
irb(main):004:2> puts "#{@a}:#{b}"
irb(main):005:2> end
irb(main):006:1> end
=3D> nil
irb(main):007:0> s =3D X.new 'hello','world'
=3D> #<struct X a=3D"hello", b=3D"world">
irb(main):008:0> s.print_attributes
:world
Notice only the value of the b attribute was printed ('world').
That's because self. was implicitly added to the front of it, so it
became self.b which will return the correct value. Changing it to:
def <=3D> other
if not a
1
elsif not b
-1
else
if a =3D=3D other.a
b <=3D> other.b
else
a <=3D> other.a
end
end
end
produces:
C:\Documents and Settings\flifson>ruby settest.rb
#<SortedSet: {#<struct X a=3D1, b=3D2>, #<struct X a=3D2, b=3D1>, #<struct =
X a=3D3, b=3D3>,
#<struct X a=3D3, b=3D4>}>
-1
1
0
My guess is that the Struct is really a type of Hash and that it uses
the method_missing system hook to actually retrieve the data from
whatever it uses to store the data. It has a lot of Hash like methods
such as values, each_pair etc etc.
Farrel