Hash -> Struct

T

Trans

Given that a hash is not ordered, is this reliable?

h = { :a=>1, :b=>2 }
Struct.new(*h.keys).new(*h.values)
=> #<struct #<Class:0xb7aead00> a=1, b=2>

T.
 
R

Robert Dober

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/
 
X

Xavier Noria

by definition not, I would never bet on it,

That's guaranteed to work in Perl. Could it be the case that it works
but it is undocumented in Ruby?

-- fxn
 
N

Nobuyoshi Nakada

Hi,

At Sun, 18 Nov 2007 02:54:12 +0900,
Robert Dober wrote in [ruby-talk:279478]:
by definition not, I would never bet on it,

Hash iterates in the definite order until it's modified, not at
random each time. It's just hard to predict. So it should
work if it's not modified between getting keys and values.
 
R

Robert Dober

Hi,

At Sun, 18 Nov 2007 02:54:12 +0900,
Robert Dober wrote in [ruby-talk:279478]:
by definition not, I would never bet on it,

Hash iterates in the definite order until it's modified, not at
random each time. It's just hard to predict. So it should
work if it's not modified between getting keys and values.
Do you believe this behavior should be "defined" in other words if I
were writing my Ruby interpreter should I implement it this way?
Personally I think it is a little bit against Hash's nature and (just
a joke Xavier) if Perl does it that way, shall we?
Cheers
Robert
 
A

Austin Ziegler

Given that a hash is not ordered, is this reliable?

h = { :a=>1, :b=>2 }
Struct.new(*h.keys).new(*h.values)
=> #<struct #<Class:0xb7aead00> a=1, b=2>

It's reliable; it's not thread-safe (making it potentially unreliable).

-austin
 
G

Gregory Seidman

Given that a hash is not ordered, is this reliable?

h = { :a=>1, :b=>2 }
Struct.new(*h.keys).new(*h.values)
=> #<struct #<Class:0xb7aead00> a=1, b=2>

It should work, but it causes me discomfort, too. I prefer this:

k,v = h.to_a.transpose
Struct.new(*k).new(*v)
--Greg
 
T

Trans

that is nice

Thanks all. Good to understand --notably the thread safety. I'll use
Robert's or Greg's suggestion.

Seems like it would be nice to have a way to generate a one time
Struct object like one can an OpenStruct. But in anycase....

Thanks,
T.
 
R

Ryan Davis

Hash iterates in the definite order until it's modified, not at
random each time. It's just hard to predict. So it should
work if it's not modified between getting keys and values.

Kinda makes me want a flag or environment variable that WILL iterate
through a hash randomly every time...

ruby --pain hash_to_struct.rb

should fail every time as written above.

It would help drive out a number of subtle and damn hard to debug bugs
out there.
 
M

MonkeeSage

It's "reliable" given the default implementation, but it is not
guaranteed for all implementations (even if the current jruby
implementaton works). Bottom line is, don't rely on implementation
details for your code to go through (if you can help it).

Regards,
Jordan
 
T

Trans

It's "reliable" given the default implementation, but it is not
guaranteed for all implementations (even if the current jruby
implementaton works). Bottom line is, don't rely on implementation
details for your code to go through (if you can help it).

Not for long though. I'm under the impression that as of 1.9,
insertion order is going to be "spec".

T.
 
M

MonkeeSage

Not for long though. I'm under the impression that as of 1.9,
insertion order is going to be "spec".

T.

Yeah, but that's still just Nobu's implementation. Until we have a
solid, implementation independent grammar (http://rubyforge.org/
projects/rubygrammar seems to be stalled), you're relying on
implementation details. But that may be all we have for a while. As
long as you're aware of that fact, go for it. :)

Regards,
Jordan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,234
Messages
2,571,179
Members
47,811
Latest member
GregoryHal

Latest Threads

Top