[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

clean nice way (hash

Shai Rosenfeld

7/16/2007 10:18:00 AM

hi,
was wondering what the prettiest way to do the below would be:

i got a hash

{ '1' => 'some', '4' => 'thing', '6' => 'good' }

and i want to turn it into

{ '1' => {'name' => 'some'}, '4' => {'name' => 'thing'}, '6' =>
{'name'=>'good'} }

--
Posted via http://www.ruby-....

23 Answers

SonOfLilit

7/16/2007 10:48:00 AM

0

hash.map{|k, v| {k => {'name' => v}}.to_hash

should work.

Not sure if there's a prettier solution.

Aur

On 7/16/07, Shai Rosenfeld <shaiguitar@gmail.com> wrote:
> hi,
> was wondering what the prettiest way to do the below would be:
>
> i got a hash
>
> { '1' => 'some', '4' => 'thing', '6' => 'good' }
>
> and i want to turn it into
>
> { '1' => {'name' => 'some'}, '4' => {'name' => 'thing'}, '6' =>
> {'name'=>'good'} }
>
> --
> Posted via http://www.ruby-....
>
>

SonOfLilit

7/16/2007 10:49:00 AM

0

Wait, no.

hash.map{|k, v| k, {'name' => v}}.to_hash

This should work.

Or
h = {}
hash.each{|k, v| h.add(k, {'name' => 'v'})

Aur

On 7/16/07, SonOfLilit <sonoflilit@gmail.com> wrote:
> hash.map{|k, v| {k => {'name' => v}}.to_hash
>
> should work.
>
> Not sure if there's a prettier solution.
>
> Aur
>
> On 7/16/07, Shai Rosenfeld <shaiguitar@gmail.com> wrote:
> > hi,
> > was wondering what the prettiest way to do the below would be:
> >
> > i got a hash
> >
> > { '1' => 'some', '4' => 'thing', '6' => 'good' }
> >
> > and i want to turn it into
> >
> > { '1' => {'name' => 'some'}, '4' => {'name' => 'thing'}, '6' =>
> > {'name'=>'good'} }
> >
> > --
> > Posted via http://www.ruby-....
> >
> >
>

hemant

7/16/2007 10:59:00 AM

0

On 7/16/07, Shai Rosenfeld <shaiguitar@gmail.com> wrote:
> hi,
> was wondering what the prettiest way to do the below would be:
>
> i got a hash
>
> { '1' => 'some', '4' => 'thing', '6' => 'good' }
>
> and i want to turn it into
>
> { '1' => {'name' => 'some'}, '4' => {'name' => 'thing'}, '6' =>
> {'name'=>'good'} }

Dunno, I can think of a couple of ways:

a.each {|key,value| a[key] = {'name' => value}}

or

a.inject({}) {|mem,(key,value)| mem[key] = {'name' => value}; mem }



--
Let them talk of their oriental summer climes of everlasting
conservatories; give me the privilege of making my own summer with my
own coals.

http://blog.g...

SonOfLilit

7/16/2007 11:16:00 AM

0

On 7/16/07, hemant <gethemant@gmail.com> wrote:
> On 7/16/07, Shai Rosenfeld <shaiguitar@gmail.com> wrote:
> > hi,
> > was wondering what the prettiest way to do the below would be:
> >
> > i got a hash
> >
> > { '1' => 'some', '4' => 'thing', '6' => 'good' }
> >
> > and i want to turn it into
> >
> > { '1' => {'name' => 'some'}, '4' => {'name' => 'thing'}, '6' =>
> > {'name'=>'good'} }
>
> Dunno, I can think of a couple of ways:
>
> a.each {|key,value| a[key] = {'name' => value}}

Are you allowed to do this?

> or
>
> a.inject({}) {|mem,(key,value)| mem[key] = {'name' => value}; mem }

Wow, thanks for showing me a new idea.


Aur

hemant

7/16/2007 11:47:00 AM

0

On 7/16/07, SonOfLilit <sonoflilit@gmail.com> wrote:
> On 7/16/07, hemant <gethemant@gmail.com> wrote:
> > On 7/16/07, Shai Rosenfeld <shaiguitar@gmail.com> wrote:
> > > hi,
> > > was wondering what the prettiest way to do the below would be:
> > >
> > > i got a hash
> > >
> > > { '1' => 'some', '4' => 'thing', '6' => 'good' }
> > >
> > > and i want to turn it into
> > >
> > > { '1' => {'name' => 'some'}, '4' => {'name' => 'thing'}, '6' =>
> > > {'name'=>'good'} }
> >
> > Dunno, I can think of a couple of ways:
> >
> > a.each {|key,value| a[key] = {'name' => value}}
>
> Are you allowed to do this?

Why not? Seem to work here.

>
> > or
> >
> > a.inject({}) {|mem,(key,value)| mem[key] = {'name' => value}; mem }
>
> Wow, thanks for showing me a new idea.
>
>
> Aur
>
>


--
Let them talk of their oriental summer climes of everlasting
conservatories; give me the privilege of making my own summer with my
own coals.

http://blog.g...

SonOfLilit

7/16/2007 12:03:00 PM

0

> > > a.each {|key,value| a[key] = {'name' => value}}
> >
> > Are you allowed to do this?
>
> Why not? Seem to work here.
>

I was taught not to modify a structure I'm #each ing on...

Does this not apply when you only modify substructures of it?


Aur

Stefan Rusterholz

7/16/2007 12:15:00 PM

0

SonOfLilit wrote:
>> > > a.each {|key,value| a[key] = {'name' => value}}
>> >
>> > Are you allowed to do this?
>>
>> Why not? Seem to work here.
>>
>
> I was taught not to modify a structure I'm #each ing on...
>
> Does this not apply when you only modify substructures of it?
>
>
> Aur

I think that only applies for insert/delete. As e.g. with an array it
may happen that elements are left out if you delete them wile iterating,
or that you iterate over a newly create value (which you might not want
to)

Regards
Stefan

--
Posted via http://www.ruby-....

SonOfLilit

7/16/2007 12:26:00 PM

0

On 7/16/07, Stefan Rusterholz <apeiros@gmx.net> wrote:
> SonOfLilit wrote:
> >> > > a.each {|key,value| a[key] = {'name' => value}}
> >> >
> >> > Are you allowed to do this?
> >>
> >> Why not? Seem to work here.
> >>
> >
> > I was taught not to modify a structure I'm #each ing on...
> >
> > Does this not apply when you only modify substructures of it?
> >
> >
> > Aur
>
> I think that only applies for insert/delete. As e.g. with an array it
> may happen that elements are left out if you delete them wile iterating,
> or that you iterate over a newly create value (which you might not want
> to)
>
> Regards
> Stefan
>
> --
> Posted via http://www.ruby-....
>
>

You just made me ponder a cool thing:

N = 8
(a = [0, 1]).each_index{|n, i| a << a[i-1] + a[i] if i > 0 and i < N

Of course it probably wouldn't work, but... Cool

Aur

James Gray

7/16/2007 2:01:00 PM

0

On Jul 16, 2007, at 5:58 AM, hemant wrote:

> a.inject({}) {|mem,(key,value)| mem[key] = {'name' => value}; mem }

I would write that as:

a.inject(Hash.new) { |h, (k, v)| h.merge(k => v) }

James Edward Gray II

Chris Carter

7/16/2007 2:19:00 PM

0

On 7/16/07, James Edward Gray II <james@grayproductions.net> wrote:
> On Jul 16, 2007, at 5:58 AM, hemant wrote:
>
> > a.inject({}) {|mem,(key,value)| mem[key] = {'name' => value}; mem }
>
> I would write that as:
>
> a.inject(Hash.new) { |h, (k, v)| h.merge(k => v) }
>
> James Edward Gray II
>
>

But that doesn't yield what he wanted... You could do
a.inject(Hash.new){|h,(k,v)| h.merge(k => { 'name' => value})}

--
Chris Carter
concentrationstudios.com
brynmawrcs.com