Given that a hash is not ordered, is this reliable?
h =3D { :a=3D>1, :b=3D>2 }
Struct.new(*h.keys).new(*h.values)
=3D> #<struct #<Class:0xb7aead00> a=3D1, b=3D2>
by definition not, I would never bet on it,
Knowing you I know that you probably know that you can do this
Struct.new( *(k =3D h.keys) ).new( *h.values_at( *k ) )
and are after something else, I however think that the above construct
might be of interest for other readers
However it seems that somehow it works in ruby and jruby
520/20 > ruby -v && jruby -v
ruby 1.8.6 (2007-06-07 patchlevel 36) [i486-linux]
ruby 1.8.5 (2007-11-01 rev 4842) [i386-jruby1.1b1]
521/21 > cat hash2strct.rb
require 'test/unit'
class TestH2S < Test::Unit::TestCase
1000.times do |n|
define_method "test_%03d" % n do
h =3D Hash.new
n.succ.times do
h["s#{rand(100)}".to_sym] =3D Object.new
end
s =3D Struct.new( *h.keys ).new( *h.values )
s.each_pair do |k,|
assert_equal s[k], h[k]
end
end
end
endrobert@roma:~/log/ruby/ML 18:28:32
517/17 > ruby hash2strct.rb
Loaded suite hash2strct
Started
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
.........................
Finished in 14.821887 seconds.
1000 tests, 90011 assertions, 0 failures, 0 errors
robert@roma:~/log/ruby/ML 18:28:55
518/18 > jruby hash2strct.rb
Loaded suite hash2strct
Started
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
...........................................................................=
.........................
Finished in 31.693 seconds.
If you really want to use this, you need confirmation of a code Guru of cou=
rse.
Cheers
Robert
--=20
what do I think about Ruby?
http://ruby-smalltalk.blogspot.com/