Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}} (http://www.velocityreviews.com/forums/t835968-time-min-sec-collect-i-i-time-now-send-i-to_i.html)

x1 12-01-2006 02:59 AM

time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}}
 
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-01-2006 03:25 AM

Re: time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}}
 
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-01-2006 03:41 AM

Re: time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}}
 
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@noaa.gov 12-01-2006 02:28 PM

Re: time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}}
 
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


x1 12-02-2006 06:35 AM

Re: time = ["min", "sec"].collect {|i| {i=> Time.now.send(i).to_i}}
 
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
>
>




All times are GMT. The time now is 02:24 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.