This is a multi-part message in MIME format.

------extPart_000_000F_01C19669.5D231FC0
Content-Type: text/plain;
	charsetso-8859-1"
Content-Transfer-Encoding: quoted-printable

also, try doing something like

h = Hash[*array]

not sure if i got the syntax correct, and nobu's to_hash is more elegant, but it was kinda cool to find out this worked as well when i asked this same question about 3 months ago

Jack



Hello,

Yohanes Santoso <ysantoso / jenny-gnome.dyndns.org> wrote:

> irb(main):011:0> myh = {"z"=>5,"x"=>3,"y"=>7}

> irb(main):012:0> myh.sort{|a,b| a[1] <=> b[1]} 

> ary = [["x", 3], ["z", 5], ["y", 7]]

^^^^^^^

Is there any "automatic" way to create hash from ary (above). I

didn't find any. (I know hash = Hash[[1,2,3,4]], but I doesn't

work for this)

Don't you think that it is strange is hash.to_a produces ary

(above), but there is now way to create hash back from this ary.

I think we should define new method

class Hash

def Hash::new_from_array(*ary)

if ary.size == 1 then ary = ary[0] end

hash = {}

ary.each do |i|

hash[i[0]] = i[1]

end

hash

end

end

Or something like this, but with achieving compatibility with

'old' style Hash[].

Michal

-- 

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Michal Rokos Czech Technical University, Prague

E-mail: m.rokos / sh.cvut.cz Jabber: majkl / jabber.cz, ICQ: 36118339

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-





------extPart_000_000F_01C19669.5D231FC0
Content-Type: text/html;
	charsetso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 5.50.4207.2601" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff><FONT size=2>
<P><FONT face=Arial>also, try doing something like</FONT></P>
<P><FONT face=Arial>h = Hash[*array]</FONT></P>
<P><FONT face=Arial>not sure if i got the syntax correct, and nobu's to_hash is 
more elegant, but it was kinda cool to find out this worked as well when i asked 
this same question about 3 months ago</FONT></P>
<P><FONT face=Arial>Jack</FONT></P>
<P><FONT face=Arial></FONT>&nbsp;</P>
<P>Hello,</P>
<P>Yohanes Santoso &lt;ysantoso / jenny-gnome.dyndns.org&gt; wrote:</P>
<P>&gt; irb(main):011:0&gt; myh = {"z"=&gt;5,"x"=&gt;3,"y"=&gt;7}</P>
<P>&gt; irb(main):012:0&gt; myh.sort{|a,b| a[1] &lt;=&gt; b[1]} </P>
<P>&gt; ary = [["x", 3], ["z", 5], ["y", 7]]</P>
<P>^^^^^^^</P>
<P>Is there any "automatic" way to create hash from ary (above). I</P>
<P>didn't find any. (I know hash = Hash[[1,2,3,4]], but I doesn't</P>
<P>work for this)</P>
<P>Don't you think that it is strange is hash.to_a produces ary</P>
<P>(above), but there is now way to create hash back from this ary.</P>
<P>I think we should define new method</P>
<P>class Hash</P>
<P>def Hash::new_from_array(*ary)</P>
<P>if ary.size == 1 then ary = ary[0] end</P>
<P>hash = {}</P>
<P>ary.each do |i|</P>
<P>hash[i[0]] = i[1]</P>
<P>end</P>
<P>hash</P>
<P>end</P>
<P>end</P>
<P>Or something like this, but with achieving compatibility with</P>
<P>'old' style Hash[].</P>
<P>Michal</P>
<P>-- </P>
<P>-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-</P>
<P>Michal Rokos Czech Technical University, Prague</P>
<P>E-mail: m.rokos / sh.cvut.cz Jabber: majkl / jabber.cz, ICQ: 36118339</P>
<P>-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-</P>
<P>&nbsp;</P></FONT>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV></BODY></HTML>

------extPart_000_000F_01C19669.5D231FC0--