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

Discussion in 'Ruby' started by x1, Dec 1, 2006.

  1. x1

    x1 Guest

    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, Dec 1, 2006
    #1
    1. Advertising

  2. x1

    x1 Guest

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

    On 11/30/06, 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']
    >
    > Thanks in advance
    >
    >
     
    x1, Dec 1, 2006
    #2
    1. Advertising

  3. x1

    x1 Guest

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

    :)


    On 11/30/06, x1 <> wrote:
    > Sorry --Subject was supposed to be "Need help creating hash from an array"
    >
    > On 11/30/06, 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']
    > >
    > > Thanks in advance
    > >
    > >

    >
    >
     
    x1, Dec 1, 2006
    #3
  4. x1

    Guest

    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
     
    , Dec 1, 2006
    #4
  5. x1

    x1 Guest

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

    Thanks so much ara!

    On 12/1/06, <> 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
    >
    >
     
    x1, Dec 2, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. The DUDE
    Replies:
    3
    Views:
    581
    code learner
    Dec 24, 2010
  2. Robocop
    Replies:
    3
    Views:
    7,070
    Sean Berry
    Feb 26, 2004
  3. Summu82
    Replies:
    5
    Views:
    867
    Richard Heathfield
    Jun 7, 2006
  4. Richard Heesbeen

    Webservice handling amount of requests per sec/min.......

    Richard Heesbeen, May 28, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    176
    Bruce Johnson [C# MVP]
    May 28, 2005
  5. cap
    Replies:
    4
    Views:
    114
Loading...

Share This Page