[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Hash -> Struct

Trans

11/17/2007 4:48:00 PM

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.

13 Answers

Robert Dober

11/17/2007 5:54:00 PM

0

On Nov 17, 2007 5:47 PM, Trans <transfire@gmail.com> wrote:
> 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

end
>

>
robert@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.blo...

Xavier Noria

11/17/2007 6:00:00 PM

0

On Nov 17, 2007, at 6:54 PM, Robert Dober wrote:

> On Nov 17, 2007 5:47 PM, Trans <transfire@gmail.com> wrote:
>> 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>
> 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


Nobuyoshi Nakada

11/17/2007 6:36:00 PM

0

Hi,

At Sun, 18 Nov 2007 02:54:12 +0900,
Robert Dober wrote in [ruby-talk:279478]:
> On Nov 17, 2007 5:47 PM, Trans <transfire@gmail.com> wrote:
> > 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>
> 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.

--
Nobu Nakada

Robert Dober

11/17/2007 6:54:00 PM

0

On Nov 17, 2007 7:35 PM, Nobuyoshi Nakada <nobu@ruby-lang.org> wrote:
> Hi,
>
> At Sun, 18 Nov 2007 02:54:12 +0900,
> Robert Dober wrote in [ruby-talk:279478]:
> > On Nov 17, 2007 5:47 PM, Trans <transfire@gmail.com> wrote:
> > > 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>
> > 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.
>
> --
> Nobu Nakada
>
>
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


--
what do I think about Ruby?
http://ruby-smalltalk.blo...

Daniel Berger

11/17/2007 7:12:00 PM

0

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

Code Snippet #1 on RubyForge. :)

http://rubyforge.org/snippet/detail.php?type=snippe...

Regards,

Dan


Austin Ziegler

11/17/2007 8:35:00 PM

0

On 11/17/07, Trans <transfire@gmail.com> wrote:
> 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
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Gregory Seidman

11/17/2007 9:07:00 PM

0

On Sun, Nov 18, 2007 at 01:47:53AM +0900, Trans wrote:
> 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)

> T.
--Greg


Robert Dober

11/17/2007 10:24:00 PM

0

On Nov 17, 2007 10:07 PM, Gregory Seidman
<gsslist+ruby@anthropohedron.net> wrote:
>
> On Sun, Nov 18, 2007 at 01:47:53AM +0900, Trans wrote:
> > 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)
that is nice
>
> > T.
> --Greg
>
>
>



--
what do I think about Ruby?
http://ruby-smalltalk.blo...

Trans

11/18/2007 11:15:00 PM

0



On Nov 17, 5:23 pm, "Robert Dober" <robert.do...@gmail.com> wrote:
> On Nov 17, 2007 10:07 PM, Gregory Seidman
>
>
>
> <gsslist+r...@anthropohedron.net> wrote:
>
> > On Sun, Nov 18, 2007 at 01:47:53AM +0900, Trans wrote:
> > > 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)
> 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.

Ryan Davis

11/19/2007 7:03:00 AM

0


On Nov 17, 2007, at 10:35 , Nobuyoshi Nakada wrote:

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

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.