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
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/ .
You must
Sign in before you can post messages.
You do not have the permission required to post.
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:
> 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/ .
You must
Sign in before you can post messages.
You do not have the permission required to post.
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:
> 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: > > 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/ .
You must
Sign in before you can post messages.
You do not have the permission required to post.
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
You must
Sign in before you can post messages.
You do not have the permission required to post.
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:
> 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}}
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
You must
Sign in before you can post messages.
You do not have the permission required to post.
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:
> On 7/16/07, hemant <gethem
... @gmail.com> wrote:
> > 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}}
> 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.gnufied.org
You must
Sign in before you can post messages.
You do not have the permission required to post.
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
You must
Sign in before you can post messages.
You do not have the permission required to post.
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/ .
You must
Sign in before you can post messages.
You do not have the permission required to post.
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:
> 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/ .
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
You must
Sign in before you can post messages.
You do not have the permission required to post.
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
You must
Sign in before you can post messages.
You do not have the permission required to post.
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
You must
Sign in before you can post messages.
You do not have the permission required to post.
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
You must
Sign in before you can post messages.
You do not have the permission required to post.
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)