Does Struct#to_a guarantee the order of the values

The Pickaxe implies this, but I thought it would be wise to make
sure…

Suppose I use Struct to generate a class and then create an instance
of the class like this:

F = Struct.new(‘F’, :x, :y, :z)
f1 = F.new(1,2,3)

Can I be sure that f1.to_a returns [1,2,3] and never [3,2,1] or
[2,3,1]? That is, does F#to_a guarantee that the values in the array
are in the same order that the instance variables were specified to
Struct.new?

Same for F#members. Will the strings in the array be in the same order
that the symbols were specified to Struct.new?

Hi,

···

In message “Does Struct#to_a guarantee the order of the values” on 03/02/22, Tim Hunter Tim.Hunter@sas.com writes:

Suppose I use Struct to generate a class and then create an instance
of the class like this:

F = Struct.new(‘F’, :x, :y, :z)
f1 = F.new(1,2,3)

Can I be sure that f1.to_a returns [1,2,3] and never [3,2,1] or
[2,3,1]? That is, does F#to_a guarantee that the values in the array
are in the same order that the instance variables were specified to
Struct.new?

Same for F#members. Will the strings in the array be in the same order
that the symbols were specified to Struct.new?

Yes. Struct is array (not hash) with symbolic index.

						matz.

Thanks, matz!

···

On Sat, 22 Feb 2003 13:58:24 +0900, Yukihiro Matsumoto wrote:

Hi,

In message “Does Struct#to_a guarantee the order of the values” > on 03/02/22, Tim Hunter Tim.Hunter@sas.com writes:

Suppose I use Struct to generate a class and then create an instance of
the class like this:

F = Struct.new(‘F’, :x, :y, :z)
f1 = F.new(1,2,3)

Can I be sure that f1.to_a returns [1,2,3] and never [3,2,1] or [2,3,1]?
That is, does F#to_a guarantee that the values in the array are in the
same order that the instance variables were specified to Struct.new?

Same for F#members. Will the strings in the array be in the same order
that the symbols were specified to Struct.new?

Yes. Struct is array (not hash) with symbolic index.

  					matz.