[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}}

x1

12/1/2006 2:59:00 AM

What's the best way to make the first item below yield a hash instead
of an array in one line? I'm struggling :(

#doesnt work
time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}}
puts time.type # Array

#works
time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}}.first
puts time.type # Hash
puts time['min']

Thanks in advance

5 Answers

x1

12/1/2006 3:26:00 AM

0

Sorry --Subject was supposed to be "Need help creating hash from an array"

On 11/30/06, x1 <caldridge@gmail.com> wrote:
> What's the best way to make the first item below yield a hash instead
> of an array in one line? I'm struggling :(
>
> #doesnt work
> time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}}
> puts time.type # Array
>
> #works
> time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}}.first
> puts time.type # Hash
> puts time['min']
>
> Thanks in advance
>
>

x1

12/1/2006 3:42:00 AM

0

ok..nvrmind.. I think I got it:
time = Hash[*["min", "sec"].collect { |i| [i, Time.now.send(i)]}.flatten]

:-)


On 11/30/06, x1 <caldridge@gmail.com> wrote:
> Sorry --Subject was supposed to be "Need help creating hash from an array"
>
> On 11/30/06, x1 <caldridge@gmail.com> wrote:
> > What's the best way to make the first item below yield a hash instead
> > of an array in one line? I'm struggling :(
> >
> > #doesnt work
> > time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}}
> > puts time.type # Array
> >
> > #works
> > time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}}.first
> > puts time.type # Hash
> > puts time['min']
> >
> > Thanks in advance
> >
> >
>
>

Ara.T.Howard

12/1/2006 2:28:00 PM

0

x1

12/2/2006 6:35:00 AM

0

ah ok. Good point. I'm learning :-)

Thanks so much ara!

On 12/1/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
> On Fri, 1 Dec 2006, x1 wrote:
>
> > What's the best way to make the first item below yield a hash instead
> > of an array in one line? I'm struggling :(
> >
> > #doesnt work
> > time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}}
> > puts time.type # Array
> >
> > #works
> > time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}}.first
> > puts time.type # Hash
> > puts time['min']
>
>
> harp:~ > cat a.rb
> now = Time.now
> h = %w( min sec ).inject({}){|h,k| h.update k => now.send(k).to_i}
>
> p h
>
>
> harp:~ > ruby a.rb
> {"sec"=>44, "min"=>26}
>
>
> in particular you don't want to call Time.now inside the loop: you'll otherwise
> sometimes get
>
> {"sec"=>0, "min"=>42}
>
> when you should have gotten
>
> {"sec"=>59, "min"=>41}
>
> regards.
>
> -a
> --
> if you want others to be happy, practice compassion.
> if you want to be happy, practice compassion. -- the dalai lama
>
>

Klaus Schadenfreude

3/9/2013 1:40:00 PM

0

>Yoorghis@Jurgis.net wrote in talk.politics.guns :

>On Fri, 08 Mar 2013 05:29:40 -0800, Klaus Schadenfreude
><klausschadenfreude@yahoo.com> wrote:
>
>>Tell us more stories about when you were stalked by the big bad
>>conservative.
>
>What?


I said, TELL US MORE ABOUT WHEN YOU WERE STALKED BY THE BIG BAD
CONSERVATIVE!!!!!!!!!!!!!!!!!