Google Groups Home
Help | Sign in
clean nice way (hash)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  24 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Shai Rosenfeld  
View profile
 More options Jul 16 2007, 6:17 am
Newsgroups: comp.lang.ruby
From: Shai Rosenfeld <shaigui...@gmail.com>
Date: Mon, 16 Jul 2007 19:17:45 +0900
Local: Mon, Jul 16 2007 6:17 am
Subject: clean nice way (hash)
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-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SonOfLilit  
View profile
 More options Jul 16 2007, 6:47 am
From: SonOfLilit <sonofli...@gmail.com>
Date: Mon, 16 Jul 2007 19:47:44 +0900
Local: Mon, Jul 16 2007 6:47 am
Subject: Re: clean nice way (hash)
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 <shaigui...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SonOfLilit  
View profile
 More options Jul 16 2007, 6:49 am
From: SonOfLilit <sonofli...@gmail.com>
Date: Mon, 16 Jul 2007 19:49:23 +0900
Local: Mon, Jul 16 2007 6:49 am
Subject: Re: clean nice way (hash)
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 <sonofli...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
hemant  
View profile
 More options Jul 16 2007, 6:58 am
From: hemant <gethem...@gmail.com>
Date: Mon, 16 Jul 2007 19:58:38 +0900
Local: Mon, Jul 16 2007 6:58 am
Subject: Re: clean nice way (hash)
On 7/16/07, Shai Rosenfeld <shaigui...@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.gnufied.org


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SonOfLilit  
View profile
 More options Jul 16 2007, 7:15 am
From: SonOfLilit <sonofli...@gmail.com>
Date: Mon, 16 Jul 2007 20:15:30 +0900
Local: Mon, Jul 16 2007 7:15 am
Subject: Re: clean nice way (hash)
On 7/16/07, hemant <gethem...@gmail.com> wrote:

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
hemant  
View profile
 More options Jul 16 2007, 7:46 am
From: hemant <gethem...@gmail.com>
Date: Mon, 16 Jul 2007 20:46:36 +0900
Local: Mon, Jul 16 2007 7:46 am
Subject: Re: clean nice way (hash)
On 7/16/07, SonOfLilit <sonofli...@gmail.com> wrote:

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.gnufied.org


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SonOfLilit  
View profile
 More options Jul 16 2007, 8:02 am
From: SonOfLilit <sonofli...@gmail.com>
Date: Mon, 16 Jul 2007 21:02:41 +0900
Local: Mon, Jul 16 2007 8:02 am
Subject: Re: clean nice way (hash)

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stefan Rusterholz  
View profile
 More options Jul 16 2007, 8:15 am
Newsgroups: comp.lang.ruby
From: Stefan Rusterholz <apei...@gmx.net>
Date: Mon, 16 Jul 2007 21:15:16 +0900
Local: Mon, Jul 16 2007 8:15 am
Subject: Re: clean nice way (hash)

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-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
SonOfLilit  
View profile
 More options Jul 16 2007, 8:26 am
From: SonOfLilit <sonofli...@gmail.com>
Date: Mon, 16 Jul 2007 21:26:09 +0900
Local: Mon, Jul 16 2007 8:26 am
Subject: Re: clean nice way (hash)
On 7/16/07, Stefan Rusterholz <apei...@gmx.net> wrote:

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Edward Gray II  
View profile
 More options Jul 16 2007, 10:00 am
From: James Edward Gray II <ja...@grayproductions.net>
Date: Mon, 16 Jul 2007 23:00:38 +0900
Local: Mon, Jul 16 2007 10:00 am
Subject: Re: clean nice way (hash)
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Carter  
View profile
 More options Jul 16 2007, 10:19 am
From: "Chris Carter" <cdcar...@gmail.com>
Date: Mon, 16 Jul 2007 23:19:28 +0900
Local: Mon, Jul 16 2007 10:19 am
Subject: Re: clean nice way (hash)
On 7/16/07, James Edward Gray II <ja...@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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Edward Gray II  
View profile
 More options Jul 16 2007, 10:35 am
From: James Edward Gray II <ja...@grayproductions.net>
Date: Mon, 16 Jul 2007 23:35:55 +0900
Local: Mon, Jul 16 2007 10:35 am
Subject: Re: clean nice way (hash)
On Jul 16, 2007, at 9:19 AM, Chris Carter wrote:

> On 7/16/07, James Edward Gray II <ja...@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})}

Right, my bad.  Thanks.

I just wanted to get away from using inject() like each().

James Edward Gray II


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile
 More options Jul 16 2007, 10:36 am
From: "Robert Klemme" <shortcut...@googlemail.com>
Date: Mon, 16 Jul 2007 23:36:04 +0900
Local: Mon, Jul 16 2007 10:36 am
Subject: Re: clean nice way (hash)